-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Has someone considered an owned trait object? #2489
Comments
The own trait object is
|
I'm actually thinking of implementing it in the opposite way. |
@lqf96 That's basically "unsized rvalues", for which the tracking issue is: rust-lang/rust#48055 |
@Ixrec Yes, such type will have to rely on unsized rvalues. What I want to propose is that object safety rules on struct A(u32);
trait Test {
fn test(self) -> u32;
}
impl Test for A {
fn test(self) -> u32 { self.0 }
} And then you can cast variable with type // Create an `A` instance and cast it to an owned trait object
let a = A(10) as dyn Test;
// Consume the owned trait object
println!("{}", a.test());
// `a` is consumed and can no longer be used With |
That is the part of the unsized rvalues RFC. In fact, it's one of the two achievements which the RFC aims at. The other one is the fast allocation of small arrays.
I'm not sure which problem you're referring, but at least the problem listed here doesn't seem to be relevant to unsized |
This seems to be completed as per the unsized rvalues RFC per notes above, thus, I'll close this issue. |
Apart from
&dyn Trait
and&mut dyn Trait
, both of which are borrowed trait objects, why don't we have an owned trait object typedyn Trait
? Such type will likely be a DST with pointers to vtables (includingDrop
), size and align of the original value, followed by the value itself. Object safety rules will be relaxed on the owned trait object type to allowself
to be consumed.Together with
DerefMove
this can be used to replaceFnBox
forBox<dyn FnOnce(...) -> ...>
syntax.The text was updated successfully, but these errors were encountered: