Skip to content

Allow dyn trait + Sized #79701

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

Closed
CreepySkeleton opened this issue Dec 4, 2020 · 4 comments
Closed

Allow dyn trait + Sized #79701

CreepySkeleton opened this issue Dec 4, 2020 · 4 comments

Comments

@CreepySkeleton
Copy link
Contributor

I would expect this code to work:

let a: Box<dyn Any + Sized> = Box::new(5);
error[E0225]: only auto traits can be used as additional traits in a trait object
  --> src/main.rs:22:26
   |
22 |     let a: Box<dyn Any + Sized> = Box::new(5);
   |                    ---   ^^^^^ additional non-auto trait
   |                    |
   |                    first non-auto trait
   |
   = help: consider creating a new trait with all of these as super-traits and using that trait here instead: `trait NewTrait: Any + Sized {}`
   = note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit <https://doc.rust-lang.org/reference/special-types-and-traits.html#auto-traits>

I realize that trait objects don't work with multiple traits because of complications with multiple vtables. Sized is also technically not an auto trait, but for all intends and purposes here (no methods in Sized - no vtables) it should be equivalent to Send or Sync or whatever.

A somewhat more realistic example:

struct Arg {
    typed_validator: Box<dyn Fn(&str) -> Result<dyn Any + Sized, Error>>
}
@jonas-schievink
Copy link
Contributor

dyn Trait can never be Sized, so this can't be made to work

@CreepySkeleton
Copy link
Contributor Author

Oh right.

@petrochenkov
Copy link
Contributor

@jonas-schievink

dyn Trait can never be Sized, so this can't be made to work

With rust-lang/rfcs#2027 accepted the requirement dyn Trait: Trait is no longer mandatory.

It makes very much sense to support dyn Sized because the traits in dyn Trait + ... are traits implemented by its underlying type, not by dyn Trait + ... itself, and the underlying type of dyn Trait + ... is always Sized currently.

@jonas-schievink
Copy link
Contributor

I see, that makes sense, yeah.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants