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

    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.


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

Search: