Search
 
SCRIPT & CODE EXAMPLE
 

RUST

armanriazi•rust•concept•pattern•newtype

Using the Newtype Pattern to Implement External Traits on External Types
' thin wrapper around the type' : part of Vec<String> is noticed. struct Wrapper(Vec<String>); 
The tuple struct will have one field and be a thin wrapper around the type we want to implement a trait for. Then the wrapper type is local to our crate, and we can implement the trait on the wrapper. Newtype is a term that originates from the Haskell programming language. There is no runtime performance penalty for using this pattern, and the wrapper type is elided at compile time.

As an example, let’s say we want to implement Display on Vec<T>, which the orphan rule prevents us from doing directly because the Display trait and the Vec<T> type are defined outside our crate. We can make a Wrapper struct that holds an instance of Vec<T>; then we can implement Display on Wrapper and use the Vec<T> value

The downside of using this technique is that Wrapper is a new type, so it doesn’t have the methods of the value it’s holding. We would have to implement all the methods of Vec<T> directly on Wrapper such that the methods delegate to self.0, which would allow us to treat Wrapper exactly like a Vec<T>. If we wanted the new type to have every method the inner type has, implementing the Deref trait (
If we don’t want the Wrapper type to have all the methods of the inner type—for example, to restrict the Wrapper type’s behavior—we would have to implement just the methods we do want manually.

use std::fmt;

struct Wrapper(Vec<String>);

impl fmt::Display for Wrapper {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "[{}]", self.0.join(", "))
    }
}

fn main() {
    let w = Wrapper(vec![String::from("hello"), String::from("world")]);
    println!("w = {}", w);
}
Comment

PREVIOUS NEXT
Code Example
Rust :: Rust Options Some None using closures 
Rust :: armanriazi•rust•error•[E0072]: recursive type `List` has infinite size -- src/main.rs:3:1 | 3 | enum List { | ^^^^^^^^^ recursive type has infinite size 
Rust :: how to check if a thing is in a vector in rust 
Rust :: rust error: failed to run custom build command for python3-sys 
Rust :: rust run tests without cargo 
Rust :: trait in rust 
Lua :: random string generator lua 
Lua :: roblox how to make a rainbow part 
Lua :: lua not equal 
Lua :: luau region 
Lua :: roblox what is the difference between __index and __newindex 
Lua :: roblox interpolate color 
Lua :: forever loop in lua 
Lua :: input in lua 
Lua :: for i = 1 to n roblox 
Lua :: how to get the length of a table in lua 
Lua :: lua indexof 
Lua :: lua string to date 
Lua :: lua push to aray 
Lua :: ex: CFrame to vector3 roblox lua 
Lua :: lua loop through string 
Lua :: lua hash keys 
Lua :: FiveM how to check where nearest player is 
Matlab :: sum vs symsum in matlab script 
Matlab :: matlab find roots of symbolic polynomial 
Basic :: random numbers visual basic 
Basic :: VBA Initialise/Initialize String/Number Array (not variant) 
Elixir :: elixir eval ast 
Scala :: scala random number 
Scala :: scala learn 
ADD CONTENT
Topic
Content
Source link
Name
1+6 =