You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
use std::any::Any;
use std::any::AnyRefExt;
struct Object { val: Box<Any> }
impl Object {
fn new_int() -> Object { Object { val: box 5 as Box<Any> } }
fn new_vec() -> Object {
let mut v: Vec<Object> = vec!();
v.push( Object::new_int() );
Object { val: box v as Box<Any> }
}
}
fn main() {
let t = Object::new_vec();
let v = t.val.as_ref::<Vec<Object>>();
}
Produces the error:
golf.rs:17:10: 17:39 error: instantiating a type parameter with an incompatible type `collections::vec::Vec<Object>`, which does not fulfill `'static`
golf.rs:17 let v = t.val.as_ref::<Vec<Object>>();
(In general, any attempts to .as_ref::<T>() where T contains Box<Any> will fail.)
I find this confusing, since box v as Box<Any> on line 11 should produce more or less the same error. Yet if you compile this with line 17 commented out, there are no errors (there are warnings about dead code/unused variables.)
Issue #7268 looks similar, but I can't convince myself that these two are the same. Line 17 doesn't ask for anything line 11 doesn't provide.
The text was updated successfully, but these errors were encountered:
Closing as working as intended. The as_ref function specifically takes <T: 'static> which means that the only invalid thing done here is the call to as_ref (not the construction).
What you need to do is to specify that Object adheres to a 'static lifetime via:
structObject{val:Box<Any + 'static>}
That way rustc can understand that the boxed trait indeed doesn't have any borrowed pointers.
Test case:
Produces the error:
(In general, any attempts to
.as_ref::<T>()
whereT
containsBox<Any>
will fail.)I find this confusing, since
box v as Box<Any>
on line 11 should produce more or less the same error. Yet if you compile this with line 17 commented out, there are no errors (there are warnings about dead code/unused variables.)Issue #7268 looks similar, but I can't convince myself that these two are the same. Line 17 doesn't ask for anything line 11 doesn't provide.
The text was updated successfully, but these errors were encountered: