Trait Borrowable

Source
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.

Implementations on Foreign Types§

Source§

impl<T: Borrowable> Borrowable for &T

Implementors§

Source§

impl<Iter> Borrowable for MultiIntersection<Iter>

Source§

impl<Iter> Borrowable for MultiUnion<Iter>

Source§

impl<Levels, Data, R> Borrowable for SparseTree<Levels, Data, R>
where Levels: SparseTreeLevels, R: DefaultRequirement,

Source§

type Borrowed = SparseTree<Levels, Data, R>

Source§

impl<S0, S1> Borrowable for Intersection<S0, S1>

Source§

impl<S0, S1> Borrowable for Union<S0, S1>

Source§

type Borrowed = Union<S0, S1>

Source§

impl<S, F> Borrowable for Map<S, F>

Source§

type Borrowed = Map<S, F>

Source§

impl<S, I, F> Borrowable for MultiMapFold<S, I, F>

Source§

impl<T, const DEPTH: usize> Borrowable for DenseTree<T, DEPTH>
where ConstUsize<DEPTH>: ConstInteger,

Source§

type Borrowed = DenseTree<T, DEPTH>