Search
 
SCRIPT & CODE EXAMPLE
 

RUST

armanriazi•rust•trait•objectsafe•vs•nonobjectsafe

Here is an example trait that is not object safe:

trait SomeTrait {
    fn foo(&self) -> int { ... }
    
    // Object-safe methods may not return `Self`:
    fn new() -> Self;
}
Splitting a trait:
One option is to split a trait into object-safe and non-object-safe parts. We hope that this will lead to better design. We are not sure how much code this will affect, it would be good to have data about this.

trait SomeTrait {
    fn foo(&self) -> int { ... }
}

trait SomeTraitCtor : SomeTrait {
    fn new() -> Self;
}
Adding a where-clause:
Sometimes adding a second trait feels like overkill. In that case, it is often an option to simply add a where Self:Sized clause to the methods of the trait that would otherwise violate the object safety rule.

trait SomeTrait {
    fn foo(&self) -> int { ... }
    
    fn new() -> Self
        where Self : Sized; // this condition is new
}
Comment

PREVIOUS NEXT
Code Example
Rust :: rust ceil 
Rust :: armanriazi•rust•error•error[E0382]: borrow of moved value: `val` ... thread 
Rust :: armanriazi•rust•concept•oop•state•pattern 
Rust :: armanriazi•rust•error•E0277•`Point<{integer}, {float}` cannot be formatted using ` 
Rust :: rust•armanriazi•loop•listen 
Rust :: rust print i8 
Rust :: rust How to use if let statement with conditions? 
Rust :: armanriazi•rust•borrowchecker•lifetime•struct 
Rust :: armanriazi•rust•binding•match 
Rust :: rust calculate every root 
Rust :: rust month to quarter 
Lua :: lua string.split 
Lua :: Get number of values in a table lua 
Lua :: roblox player joined 
Lua :: luau how to make debounce 
Lua :: roblox lua gui drag 
Lua :: input in lua 
Lua :: string to int lua 
Lua :: roblox number between 1 and 10 
Lua :: lua toggle 
Lua :: Lua How to check what index belongs to value 
Lua :: roblox rotate head with camera 
Lua :: can you throw an error forceable in lua 
Lua :: how to make a run in roblox lua 
Lua :: roblox lua 
Matlab :: matlab not less than 
Matlab :: matlab avoid plot to get focus 
Basic :: powershell how to removve only empty direcoties 
Elixir :: elixir length of list 
Elixir :: elixir enum chunk_every 
ADD CONTENT
Topic
Content
Source link
Name
9+7 =