Trying to solve the problem described in Trait bound Sized is not satisfied for Sized trait, I found the following code gives the following error:
trait SizedTrait: Sized { fn me() -> Self; } trait AnotherTrait: Sized { fn another_me() -> Self; } impl AnotherTrait for SizedTrait + Sized { fn another_me() { Self::me() } }
error[E0225]: only auto traits can be used as additional traits in a trait object --> src/main.rs:9:36 | 9 | impl AnotherTrait for SizedTrait + Sized { | ^^^^^ non-auto additional trait
But the Rust Book does not mention auto trait
at all.
What is an auto trait in Rust and how does it differ from a non-auto trait?
First, auto traits are automatically implemented using the following rules: &T , &mut T , *const T , *mut T , [T; n] and [T] implement the trait if T does. Function item types and function pointers automatically implement the trait. Structs, enums, unions and tuples implement the trait if all of their fields do.
A trait object is an opaque value of another type that implements a set of traits. The set of traits is made up of an object safe base trait plus any number of auto traits. Trait objects implement the base trait, its auto traits, and any supertraits of the base trait.
A trait is a way to define shared behavior in Rust. As Rust by Example puts it: A trait is a collection of methods defined for an unknown type: Self . They can access other methods declared in the same trait.
The Sized trait in Rust is an auto trait and a marker trait. Auto traits are traits that get automatically implemented for a type if it passes certain conditions. Marker traits are traits that mark a type as having a certain property.
An auto trait is the new name for the terribly named1opt-in, built-in trait (OIBIT).
These are an unstable feature where a trait is automatically implemented for every type unless they opt-out or contain a value that does not implement the trait:
#![feature(optin_builtin_traits)] auto trait IsCool {} // Everyone knows that `String`s just aren't cool impl !IsCool for String {} struct MyStruct; struct HasAString(String); fn check_cool<C: IsCool>(_: C) {} fn main() { check_cool(42); check_cool(false); check_cool(MyStruct); // the trait bound `std::string::String: IsCool` is not satisfied // check_cool(String::new()); // the trait bound `std::string::String: IsCool` is not satisfied in `HasAString` // check_cool(HasAString(String::new())); }
Familiar examples include Send
and Sync
:
pub unsafe auto trait Send { } pub unsafe auto trait Sync { }
Further information is available in the Unstable Book.
1 These traits are neither opt-in (they are opt-out) nor necessarily built-in (user code using nightly may use them). Of the 5 words in their name, 4 were outright lies.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With