Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

One solution is to put the Future into a Box.


And I can't believe I missed this, but the other way is to make the struct generic over a type implementing Future.


Does this work though? How do you create an instance of the struct without an instance of the Future?


    trait 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.




Consider applying for YC's Winter 2026 batch! Applications are open till Nov 10

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: