|
MALE SHOEGAZE posted:I did some rust this weekend and I will feel pretty convinced that it's going to be a big deal. Quick pick a top library from another language and then write it in Rust! Parsec, lens, an xml parser or something.
|
# ¿ Jun 14, 2015 20:24 |
|
|
# ¿ Apr 28, 2024 14:08 |
|
Does anybody want to critique my babby's first data structure? http://codepad.org/SG2ERyJn There were a few things that I tried to do and failed. I know that the methods should probably be implemented in an impl with a &self parameter but I'm ignoring that for now.
|
# ¿ Feb 17, 2016 00:09 |
|
Thanks very much, that's enough to get me started. I've decided to go back and finish the Rust introduction though, as I'm hoping things like into_iter will he explained by its end. E: let's just say that I'm getting a renewed sense of the frustrations that people often feel when learning haskell gonadic io fucked around with this message at 13:50 on Feb 19, 2016 |
# ¿ Feb 19, 2016 13:37 |
|
It works! https://play.rust-lang.org/?gist=3c137f16de010859622b&version=stable And after only 2 and a half glasses of wine too! This is step 1 on my plan for e: and now to implement ray casting... although I am cheating slightly because I've already implemented all of this in F# before I restarted in Scala and now I'm restarting again in Rust so I'm pretty familiar with the actual logic. gonadic io fucked around with this message at 00:33 on Feb 20, 2016 |
# ¿ Feb 19, 2016 23:44 |
|
Not that I've actually done it yet, but Rust's FFI can be used in place of a C++ external function as described by this blog post:code:
And then F# in place of C# is trivial although I might need one or two C# wrappers if Unity demands C# in places? I'm not sure there either. Basically this is a fun adventure where I throw myself into the deep end and learn some new technologies/languages! I've never used Rust before, nor Unity, the only non-GC'd language I've used before is 1-2 tiny toy C programs, and I haven't really done all that much in F# before. I am however pretty good at Haskell gonadic io fucked around with this message at 00:32 on Feb 20, 2016 |
# ¿ Feb 20, 2016 00:23 |
|
Is there a better way to get the first Some in a list of Options (and if there isn't any, return None) than this?code:
If anybody is interesting in my ongoing project, the ray casting functionality is done now. Also the file's reached 200 lines so I guess I should probably split it into modules and stuff. Any pointers or references are appreciated: https://gist.github.com/anonymous/224fcfb0e171941715a6 gonadic io fucked around with this message at 19:25 on Feb 21, 2016 |
# ¿ Feb 21, 2016 19:10 |
|
FamDav posted:map is lazy though: https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.map What I have (map and find) works and is short-circuiting, I was just wondering if there was a cleaner solution like [].cat_maybes().head_option() or something. gonadic io fucked around with this message at 20:16 on Feb 21, 2016 |
# ¿ Feb 21, 2016 20:04 |
|
Today I wrote the line code:
|
# ¿ Mar 5, 2016 22:24 |
|
Is this a reasonable way to pass an optional value over a FFI boundary? Passing a (nullable) pointer and then having the C# code calling another function to free it seems like lots of pain.code:
gonadic io fucked around with this message at 22:04 on Mar 13, 2016 |
# ¿ Mar 13, 2016 22:00 |
|
I have a weird and pretty gross function that I can't help but think "surely there must be a better way!" about :code:
As ever, the context is here.
|
# ¿ Mar 19, 2016 01:31 |
|
Perfect, thanks. e: is there a better way to represent once-assignable nullable variables than Cell<Option<_>>? The type allows for re-assignation but I will never do that. I suppose I could always just put a wrapper around it. gonadic io fucked around with this message at 11:26 on Mar 19, 2016 |
# ¿ Mar 19, 2016 10:52 |
|
Hi all, thanks loads for your previous help. I'm having another fight with the borrow checker again though. In short, what I want to do is this: code:
Full context is here. gonadic io fucked around with this message at 23:47 on Mar 22, 2016 |
# ¿ Mar 22, 2016 23:29 |
|
Yep, that was exactly it thanks. So even though the closure is lexically out of scope, because the iterator hasn't been evaluated yet the compiler still considers it to be in scope for the purposes of borrowing? E: I think I get it now - the iterator borrows the reference because the closure does, and the iterator is still in scope (regardless of how much it was evaluated) which is causing the problem. gonadic io fucked around with this message at 10:24 on Mar 23, 2016 |
# ¿ Mar 23, 2016 09:39 |
|
Are people happy for me to continue asking this stuff here? I don't want to dominate the thread, I guess I could go to the terrible programmers yospos thread or the rust IRC instead. Speaking of, I'm running into a situation with the FFI that I'm pretty sure is down to me using it incorrectly. My Rust code looks like: code:
test deregister start from unity deregistering 0 test deregister end However in other FFI functions, i.e. code:
about to deregister in other function rust callback deregister_voxel 0 <end of log> So from what I can see the callback works fine when called initially, but when the address is called again later it crashes with Thread 0 Crashed:: CrBrowserMain Dispatch queue: com.apple.main-thread 0 ??? 0x00007fff5fbfc860 0 + 140734799792224 1 libcarved_rust.bundle 0x000000011eeef77c svo_set_block + 28 I must say that debugging FFI is really quite frustrating because Unity just crashes with no message, the "send to Apple?" window contains the message above and then the Unity log contains the tracing statements. So how should I keep these functions around and allow them to be called just given the pointer to the ExternalSVO. Full context (including the definition of ExternalSVO) is on my github. gonadic io fucked around with this message at 20:40 on Mar 27, 2016 |
# ¿ Mar 27, 2016 20:23 |
|
I moved the closure to the internal function like so:code:
I tried a few times without the intermediate (seemingly no-op) closure there by casting the extern "stdcall" fn to both &Fn(...) -> u32 and Box<Fn(...) -> u32> and then passing them directly to set_blocks which expects a &Fn but neither worked. I got either carved_rust.rs:43:13: 43:35 error: the trait `core::ops::Fn<(nalgebra::structs::vec::Vec3<f32>, i32, svo::VoxelData)>` is not implemented for the type `extern "stdcall" fn(nalgebra::structs::vec::Vec3<f32>, i32, svo::VoxelData) -> u32` [E0277] or carved_rust.rs:45:9: 45:44 error: the trait `core::ops::Fn<(nalgebra::structs::vec::Vec3<f32>, i32, svo::VoxelData)>` is not implemented for the type `Box<core::ops::Fn(nalgebra::structs::vec::Vec3<f32>, i32, svo::VoxelData) -> u32>` [E0277] Is the Fn trait implemented for extern "stdcall" fn? Or is it something to do with the missing "-> u32"?
|
# ¿ Mar 27, 2016 22:56 |
|
Vanadium posted:(fwiw people tend to pass closures by value when possible and I don't recall having seen &|...|) The reason I'm doing this is that inside set_blocks they get cloned and passed around a whole bunch and so references are needed. I have at least made it so that the public function takes them by value. Honestly I'm hoping that the compiler will optimise away passing the references to every invocation of set_voxel_from. The way I'd do this in Haskell would be to define the sub-function as a closure inside the top-level function which just uses the register and deregister functions in scope but apparently in Rust you can't define recursive closures or fns inside another fn. code:
gonadic io fucked around with this message at 11:00 on Mar 28, 2016 |
# ¿ Mar 28, 2016 09:55 |
|
I have lot of fixed sized arrays. Functions that return [T; 8] etc. Working with them is a real pain since neither FromIterator nor ToIterator are implemented for them. I feel that things like map and collect should be available but it's pretty cumbersome converting between slices in order to use these functions. Is there a recommended way for working with arrays rather than slices or should I not bother? e: for a concrete example I have code:
code:
I tried at first to use some flat_maps, but couldn't figure that out and instead am now trying to do it explicitly with pattern matching but still haven't managed yet. Is there a nice way to do this? Should I just use slices and forgo the compile-time guarantees? gonadic io fucked around with this message at 20:18 on Apr 4, 2016 |
# ¿ Apr 4, 2016 20:13 |
|
Presented without comment:code:
|
# ¿ Apr 4, 2016 20:46 |
|
gonadic io posted:
I had a chance to try and combine these two functions tonight and am still fighting the borrow checker. SubImage implements Copy and Clone, so why I'm getting reference not living long enough errors even when I tried to clone() everything is beyond me. I get that quads goes out of scope at the end of the closure so any references to it would be invalid, but I'm copying all of the SubImages right? Making it a move closure doesn't help either. Honestly I feel that this entire thing could be done as a few flat_maps but I've yet to get it to compile even 1) doing a bunch of manual indexing and 2) pretending that split_threshold can never fail. code:
reference must be valid for the anonymous lifetime #1 defined on the block at 63:45... (the entire function octs) ...but borrowed value is only valid for the scope of parameters for function at 64:37 (the closure)
|
# ¿ Apr 7, 2016 23:00 |
|
I see now, it's because SubImage contains a reference to a byte buffer.code:
I tried adding 'a lifetimes everywhere to all the references and SubImage but I guess that was being done implicitly by the compiler anyway. e: when I next get internet I'll push to github so you can see the whole code e2: https://github.com/djmcgill/carved/blob/master/carved_rust/src/svo/generator/height_map.rs gonadic io fucked around with this message at 09:44 on Apr 8, 2016 |
# ¿ Apr 8, 2016 08:44 |
|
That makes perfect sense, thanks all. I'll sort it out tonight. Of course, given how much of a pain using map with arrays is I might just stick with my manual indexing. That or write a map_8 function.
|
# ¿ Apr 8, 2016 11:31 |
|
I own a [T; 8] and would like to access the Ts out of it without copying (this is important). Then I'd like to call a FnOnce(T) -> T on each one and package them back up in an array. I managed to cobble together a working version thanks to SO: code:
However I have the function: code:
code:
I get the error error: cannot borrow data mutably in a captured outer variable in an `Fn` closure [E0387]. Is there any way I can 1) bypass needing a mutable pointer to an outer variable in the closure, or 2) have a mutable pointer to an outer variable in the closure? Full context is here: https://github.com/djmcgill/carved/blob/master/carved_rust/src/svo/mod.rs gonadic io fucked around with this message at 19:44 on Apr 20, 2016 |
# ¿ Apr 20, 2016 19:41 |
|
It might well entirely be that I'm trying to do the wrong thing here too. The core problem is that I have a large tree of nested box pointers to SVO<Registered>, and I'd like to end with SVO<Unregistered>. Given that this is a potentially huge structure I'd like to do it with as little copying as possible so I've got deregister taking self by value as I'll never want to access the old SVO. The leaf case is easy, but given the array [Box<SVO<Registered>>; 8] I'd like to call deregister on each of them recursively (to get 8 Box<SVO<Unregistered>>s) and then collect them up. Given that it's only 8 pointers I'm not that fussed about reusing the old array's memory (but I suppose if I can I will). What I was asking in my post was how to avoid just manually indexing 8 times but if my approach is wrong I'd like to know how I could do it better!. However, changing new_octants to require a FnMut rather than a Fn seems to work perfectly. This is what I've got now, does it make sense? Does having the index be u8 actually help at all? The index can only be 0-7 so I kind of would like a u3 type so that invalid values can't be represented but then I always end up converting to usize to do any actual indexing. code:
gonadic io fucked around with this message at 10:03 on Apr 21, 2016 |
# ¿ Apr 21, 2016 10:00 |
|
What about the best of both worlds?code:
|
# ¿ Apr 21, 2016 20:56 |
|
I'm having quite a lot of trouble with what the types of closures are. I havecode:
My current error is: code:
Also I tried to make the functions inside the RegistrationFunctions object be references instead of on the heap but apparently the references to the Fn types might outlive the Fns themselves? Do Fns have a hidden lifetime parameter? e: to recall, Unregistered has the _padding parameter so that I can potentially transmute between Registered and Unregistered.
|
# ¿ May 19, 2016 13:07 |
|
I reread the closure tutorial and finally grokked the comment about how closures are implemented by the compiler constructing bespoke structs for each one and then implementing the Fn traits. So after just Boxxing everything, I ended up with code:
|
# ¿ May 22, 2016 12:59 |
|
Actually this didn't quite work, and the Boxes required lifetime parameters like so: (otherwise the closures were given static lifetimes) code:
gonadic io fucked around with this message at 16:10 on May 22, 2016 |
# ¿ May 22, 2016 15:55 |
|
I'm actually running into a real design issue here around FFI: I'd like to deregister a SVO, changing it's type code:
code:
|
# ¿ May 22, 2016 17:26 |
|
Here's what I use:code:
|
# ¿ Nov 13, 2016 13:47 |
|
MALE SHOEGAZE posted:boo! I tried that but I was doing parse_from_bytes<MyMessageType>(m). The parse errors probably should have clued me in. I'm pretty sure all Rust I've ever written contains ".unwrap() // TODO" and all haskell "fromJust x -- TODO"
|
# ¿ Dec 18, 2016 23:43 |
|
Asymmetrikon posted:I think my least favorite thing about refactoring so far has been trying to isolate parts of a chain of iterators. The result of a .map().filter().whatever() has a gnarly result type. Will 'impl trait' help for this case?
|
# ¿ Jan 3, 2017 22:22 |
|
Asymmetrikon posted:Yeah. Is that out of nightly yet? Nope! And with good reason, there's still plenty of edge cases and bugs that are falling out. The main functionality works fine though. Dehumanise yourself and face to nightly. Or just make extensive use of type inference, either adding no explicit type sig, or use lots of underscores in it.
|
# ¿ Jan 5, 2017 12:20 |
|
On the plus side I got rust's cross-compiling intrinsics working. On the downside now I have this in my code:Rust code:
gonadic io fucked around with this message at 22:30 on Jan 28, 2017 |
# ¿ Jan 28, 2017 22:24 |
|
ShoulderDaemon posted:If those are symbols coming from your linker script, they shouldn't have the type "usize", by the way. "()" is way more appropriate, because there's no data backing them. In the C code I'm looking at they're all defined "extern unsigned int" but I suppose that's just bad practice. I figured out a way to take advantage of the auto-coerce: code:
|
# ¿ Jan 29, 2017 09:23 |
|
"static mut __etext: ();" triggers code:
For now honestly I think that I'm just going to disable that warning for those definitions as, unlike other sizes of tuples, the memory layout of () is guaranteed. gonadic io fucked around with this message at 10:58 on Jan 29, 2017 |
# ¿ Jan 29, 2017 10:52 |
|
I use Visual Studio Code and I like it a lot. It has support for auto-builds, syntax highlighting, error underlining, name lookup, autocomplete (some amount anyway), formatting code and stuff. No semantic rename yet though sadly. https://github.com/saviorisdead/RustyCode e: if you want something leaner and are willing to put up with 1) nagging to pay, and 2) much worse rust support (but still syntax highlighting and goto definition) then there's always sublime gonadic io fucked around with this message at 23:14 on Jan 31, 2017 |
# ¿ Jan 31, 2017 22:38 |
|
Hmm, I'm running into a problem that isn't complex in itself but I'm sure that my solution is really unidiomatic. I'm continuing with my rust-for-arduino project and my RTC alarm enum looks like thiscode:
Since Rust enums aren't fully-fledged objects that can have static members for each variant like Scala, and if-let blocks don't allow multiple patterns, and I don't really want to write and implement a HasSeconds/HasMinutes trait for this single method I went with this approach (which I don't really like): code:
Full snippet is here: https://gist.github.com/djmcgill/91556f58d1aedff2adc7428a6443aac2 Full code is here: https://github.com/djmcgill/to-the-moon/blob/master/src/component/rtc/mod.rs#L142
|
# ¿ Mar 9, 2017 15:03 |
|
That is much better thanks, although making a new file and enum for every method is going to bloat my code somewhat... I went with the explicit approach over the recursive macro for maintenance /ease of understanding purposes. Next question: I need to be able to attach a callback to the alarm interrupt. The way this is done in the C is to have a global mutable function pointer. When the alarm goes off if the pointer isn't null then execute the function. I think I can use a static mut AtomicPtr<&'static Fn() > for this. I can't use Mutex or lazy_static because they both depend on std. Amy better ways present themselves? e: hmm, I could at least make the pointer private and the callback a parameter to the set_alarm method. gonadic io fucked around with this message at 12:45 on Mar 10, 2017 |
# ¿ Mar 10, 2017 12:26 |
|
Is there a way to generate an identifier inside a macro? I have a macro that creates a struct that implements a trait (similar to closure structs implementing Fn) code:
code:
Full code is here. gonadic io fucked around with this message at 16:30 on Mar 22, 2017 |
# ¿ Mar 22, 2017 16:16 |
|
|
# ¿ Apr 28, 2024 14:08 |
|
Ralith posted:Given that rust macros are intended to be hygenic, what happens if you just give the struct a fixed identifier directly? If it works at all I imagine it'll be scoped to the macroexpansion. That worked perfectly actually, thanks. Also because the struct is created inside the macro body expression anyway: code:
edit: is it best practice to have the expansion be code:
|
# ¿ Mar 22, 2017 18:25 |