Trait enso_prelude::ContentRef[][src]

pub trait ContentRef: HasContent {
    fn content(&self) -> &Self::Content;
}
Expand description

Unwrapping utility for wrapped types.

Please note that this trait is very similar to the Deref trait. However, there is a very important difference. Unlike Deref, there is no impl<'a, T> Unwrap for &'a T defined. The existence of such impl is very error prone when writing complex impls. The Deref docs warn about it explicitly: “[…] Because of this, Deref should only be implemented for smart pointers to avoid confusion.”. As an example, consider the following code which contains infinite loop:

pub trait HasId {
    fn id(&self) -> usize;
}

// Notice the lack of bound `<T as Deref>::Target : HasId`
impl<T:Deref> HasId for T {
    fn id(&self) -> usize {
        self.deref().id()
    }
}

And the correct version:

pub trait HasId {
    fn id(&self) -> usize;
}

// Notice the lack of bound `<T as Deref>::Target : HasId`
impl<T:Deref> HasId for T where <T as Deref>::Target : HasId {
    fn id(&self) -> usize {
        self.deref().id()
    }
}

Both versions compile fine, but the former loops for ever.

Required methods

fn content(&self) -> &Self::Content[src]

Unwraps this type to get the inner value.

Implementors

impl<T: ?Sized> ContentRef for Rc<T>[src]

fn content(&self) -> &Self::Content[src]