3 ms·
Does this work though? How do you create an instance of the struct without an instance of the Future?
by ridiculous_fish 10y ago
Does this work though? How do you create an instance of the struct without an instance of the Future?
- steveklabnik 10y agotrait Future {} struct DynamicFoo { text:String, validating: Option<Box<Future>> } struct StaticFoo<T: Future> { text:String, validating: Option<T> } These are the two options. In the first, Box<Future> stores a "trait object", that is, a tuple of (data ptr, vtable ptr). In the second, there will be a struct for each type used to generate a StaticFoo. impl Future for i32 {} impl Future for f64 {} fn main() { let d = DynamicFoo { text: String::from("foo"), validating: Some(Box::new(5) as Box<Future>), }; let s1 = StaticFoo { text: String::from("foo"), validating: Some(5), }; let s2 = StaticFoo { text: String::from("foo"), validating: Some(5.0), }; } Works just fine.