[−][src]Module fastobo::share
Enhanced Borrow
, ToOwned
and Cow
to use in tree structures.
This module define two traits:
Share
, related tostd::borrow::Borrow
Redeem
, related tostd::borrow::ToOwned
as well as theCow
enum with an interface close tostd::borrow::Cow
.
The main differences with traits from std::borrow
are that:
Share::share
does not need to return a reference.Share::share
has access to the lifetime of the referenced owner.Redeem<Owned=T> for B
does not requireimpl Share<B> for T
Cow
supports owning structures (preferablyShare
d ones).Cow
needs explicit references where applicable (e.g.Cow<'a, &'a str>
).
This allow a Cow
-like behaviour on enum structures referencing pointers.
Examples
use fastobo::share::{Cow, Redeem, Share}; pub struct MyOwner(String); #[derive(Clone)] pub struct MyRef<'a>(Cow<'a, &'a str>); impl<'a> Share<'a, MyRef<'a>> for MyOwner { fn share(&'a self) -> MyRef<'a> { MyRef(Cow::from(self.0.as_ref())) } } impl<'a> Redeem<'a> for MyRef<'a> { type Owned = MyOwner; fn redeem(&'a self) -> Self::Owned { MyOwner(self.0.to_string()) } }
Enums
Cow |
Traits
Redeem | A trait for taking ownership over viewed data with possibly expensives costs. |
Share | A trait for obtaining a data view from a reference to an owned |