This fascinating snippet of Rust does some tricky work with no allocations or copying, type- and memory-safely: https://gist.github.com/Aatch/5734372
@andy_matuschak it at least allocates the Some() on line 59, right? Very cool, though.
-
-
@avibryant Ah, sorry, I wasn’t counting stack allocations because they’re not what I normally worry about. Yes. -
@andy_matuschak hm, how does that Some avoid being heap allocated? It gets returned from the get_string call. (I don't know Rust well). -
@avibryant The Some wraps a reference to an inner pointer of the json argument. The “lifetime parameters” ensure the safety of that trick. -
@andy_matuschak right, that part I understood, and is very neat. But isn't the wrapper itself a new allocation? -
@avibryant It’s like this in C: typedef union { void *Some; bool isNothing } Option Option foo(void *x) { return (Option) {.Some = x}. } -
@avibryant (except much safer in a number of ways) In that snippet, a structure is being returned on the stack, like the Option in my C. -
@andy_matuschak got it, thank you. Out of curiosity: does the compiler have special knowledge of Option, or is it library code? -
@avibryant No, it’s library code. Nothing special. https://github.com/mozilla/rust/blob/master/src/libcore/option.rs …
End of conversation
New conversation -
Loading seems to be taking a while.
Twitter may be over capacity or experiencing a momentary hiccup. Try again or visit Twitter Status for more information.