Re: [rust-dev] Dynamic in Rust
You could define an enum that encapsulates all known types. enum monster { Integer(int), Float(float), } Then use a container for this type. On Aug 23, 2013 10:20 AM, Oren Ben-Kiki o...@ben-kiki.org wrote: Is it possible to implement something like Haskell's Dynamic value holder in Rust? (This would be similar to supporting C++'s dynamic_cast). Basically, something like this: pub struct Dynamic { ... } impl Dynamic { pub fn put(value: ~T) { ... } pub fn get() - OptionT { ... } } I guess this would require unsafe code... even so, it seems to me that Rust pointers don't carry sufficient meta-data for the above to work. A possible workaround would be something like: pub struct Dynamic { type_name: ~str, ... } impl Dynamic { pub fn put(type_name: str, value: ~T) { Dynamic { type_name: type_name, ... } } pub fn get('a self, type_name: str) - Option'a T { assert_eq!(type_name, self.type_name); ... } } } And placing the burden on the caller to always use the type name int when putting or getting `int` values, etc. This would still require some sort of unsafe code to cast the `~T` pointer into something and back, while ensuring that the storage for the `T` (whatever its size is) is not released until the `Dynamic` itself is. (Why do I need such a monstrosity? Well, I need it to define a `Configuration` container, which holds key/value pairs where whoever sets a value knows its type, whoever gets the value should ask for the same type, and the configuration can hold values of any type - not from a predefined list of types). Is such a thing possible, and if so, how? Thanks, Oren Ben-Kiki ___ Rust-dev mailing list Rust-dev@mozilla.org https://mail.mozilla.org/listinfo/rust-dev ___ Rust-dev mailing list Rust-dev@mozilla.org https://mail.mozilla.org/listinfo/rust-dev
Re: [rust-dev] Dynamic in Rust
I think your second solution is what will work. You can use unsafe code and a void pointer combined with a type descriptor string. I would probably combine the void pointer and string in a struct. On Aug 23, 2013 10:27 AM, Oren Ben-Kiki o...@ben-kiki.org wrote: That would require me to declare up front which types were usable and which weren't. I'm looking for a solution where I don't need to do that; that is, allow me to add new components to the system with new configuration parameter types, without having to go to a central source location and declare these types there. On Fri, Aug 23, 2013 at 7:54 AM, Abhijeet Gaiha abhijeet.ga...@gmail.comwrote: You could define an enum that encapsulates all known types. enum monster { Integer(int), Float(float), } Then use a container for this type. On Aug 23, 2013 10:20 AM, Oren Ben-Kiki o...@ben-kiki.org wrote: Is it possible to implement something like Haskell's Dynamic value holder in Rust? (This would be similar to supporting C++'s dynamic_cast). Basically, something like this: pub struct Dynamic { ... } impl Dynamic { pub fn put(value: ~T) { ... } pub fn get() - OptionT { ... } } I guess this would require unsafe code... even so, it seems to me that Rust pointers don't carry sufficient meta-data for the above to work. A possible workaround would be something like: pub struct Dynamic { type_name: ~str, ... } impl Dynamic { pub fn put(type_name: str, value: ~T) { Dynamic { type_name: type_name, ... } } pub fn get('a self, type_name: str) - Option'a T { assert_eq!(type_name, self.type_name); ... } } } And placing the burden on the caller to always use the type name int when putting or getting `int` values, etc. This would still require some sort of unsafe code to cast the `~T` pointer into something and back, while ensuring that the storage for the `T` (whatever its size is) is not released until the `Dynamic` itself is. (Why do I need such a monstrosity? Well, I need it to define a `Configuration` container, which holds key/value pairs where whoever sets a value knows its type, whoever gets the value should ask for the same type, and the configuration can hold values of any type - not from a predefined list of types). Is such a thing possible, and if so, how? Thanks, Oren Ben-Kiki ___ Rust-dev mailing list Rust-dev@mozilla.org https://mail.mozilla.org/listinfo/rust-dev ___ Rust-dev mailing list Rust-dev@mozilla.org https://mail.mozilla.org/listinfo/rust-dev
[rust-dev] Static initialisation of LinearMap
Hi Folks, Is there a way to statically initialise a LinearMap in code? Something like you can do with a vector. Any other suggestions to save time for inserting a fixed set of values into a hash map? Thanks, Abhijeet -- Abhijeet Gaiha http://about.me/abhijeet.gaiha ___ Rust-dev mailing list Rust-dev@mozilla.org https://mail.mozilla.org/listinfo/rust-dev
Re: [rust-dev] Static initialisation of LinearMap
If I did write a macro, that would amount to the same thing as doing the inserts at run-time (I think). I was looking for something like: static h:HashMapK,C = {(K,V),(K,V).}. Is this possible at all? Any potential equivalents in the std/ext library to just writing a custom map of my own to do the same? -- Abhijeet Gaiha http://about.me/abhijeet.gaiha On Tuesday, 2 July 2013 at 7:10 PM, Corey Richardson wrote: On Tue, Jul 2, 2013 at 9:26 AM, Abhijeet Gaiha abhijeet.ga...@gmail.com (mailto:abhijeet.ga...@gmail.com) wrote: Hi Folks, Is there a way to statically initialise a LinearMap in code? Something like you can do with a vector. Any other suggestions to save time for inserting a fixed set of values into a hash map? LinearMap is now std::hashmap::HashMap (in 0.7 and in master). There is no way to have a *static* hashmap, I think (`static h: HashMapK, V = HashMap::new()`, would be immutable and useless), but you could write a macro that expands to a bunch of inserts into a hashmap. Not sure, though. ___ Rust-dev mailing list Rust-dev@mozilla.org https://mail.mozilla.org/listinfo/rust-dev
Re: [rust-dev] Mutability and borrowing
Trying to concurrently borrow a value twice is never going to work. You could write a function like double for such a situation. -- Abhijeet Gaiha http://about.me/abhijeet.gaiha On Sunday, 2 June 2013 at 10:03 AM, Ziad Hatahet wrote: I have the following function: fn add_equal(x: mut Complex, y: Complex) { x.real += y.real; x.imag += y.imag; } Calling the function with the same variable being passed to both arguments (i.e. add_equal(mut c, c)), results in the compile error: error: cannot borrow `c` as immutable because it is also borrowed as mutable I am guessing this is to avoid aliasing issues? What is the way around this? Thanks -- Ziad ___ Rust-dev mailing list Rust-dev@mozilla.org (mailto:Rust-dev@mozilla.org) https://mail.mozilla.org/listinfo/rust-dev ___ Rust-dev mailing list Rust-dev@mozilla.org https://mail.mozilla.org/listinfo/rust-dev
[rust-dev] String Internment Library
There is an issue on Servo #282, which talks about having a uniform string internment strategy across Rust, Spidermonkey and NetsurfCSS. Is there any existing string internment library in Rust? If not, any plans to provide one? ___ Rust-dev mailing list Rust-dev@mozilla.org https://mail.mozilla.org/listinfo/rust-dev
[rust-dev] TCP Library Selection
Hi, Looks like there are three different libraries for TCP socket functionality: Net_tcp Uv_ll Net::tcp Could someone enlighten me as to which one is the recommended library to use? Thanks very much! Regards, Abhijeet ___ Rust-dev mailing list Rust-dev@mozilla.org https://mail.mozilla.org/listinfo/rust-dev