3 ms·
One solution is to put the Future into a Box.
by steveklabnik 10y ago
One solution is to put the Future into a Box.
- steveklabnik 10y agoAnd I can't believe I missed this, but the other way is to make the struct generic over a type implementing Future.
- ridiculous_fish 10y agoDoes 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.