pub trait Borrowable: Borrow<Self::Borrowed> {
type Borrowed;
}
Expand description
Borrowable
means it can be either T or &T.
Using this over Borrow, allow accepting T or &T as argument with type-deduction:
#[derive(Debug)]
struct S;
impl Borrowable for S {type Borrowed = S;}
fn test(v: impl Borrowable<Borrowed: Debug>){
println!("{:?}", v.borrow());
}
fn main(){
test(S);
test(&S);
}
While Borrow will fail to compile for this case:
ⓘ
#[derive(Debug)]
struct S;
fn test<S: Debug>(v: impl Borrow<S>){
println!("{:?}", v.borrow());
}
fn main(){
test(S);
test(&S); // error: type annotations needed.
// cannot infer type for type parameter `S` declared on the function `test`
}
Required Associated Types§
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.