pub struct Impl<T>(/* private fields */);
Expand description
Wrapper type for targeting and accessing actual implementation.
Impl has smart-pointer capabilities, as it implements [std::ops::Deref] and [std::ops::DerefMut].
You may freely choose what kind of T
you want to wrap. It may be an owned one or it could be
a &T
. Each have different tradeoffs.
An owned T
is the most flexible in implementations, but that requires always owning “sub-implementations”
through an Impl
:
use implementation::Impl;
struct MyConfig {
param1: i32,
sub_config: Impl<SubConfig>,
}
struct SubConfig {
param2: i32,
}
A referenced &T
makes it possible to borrow an Impl
from any T
, but that could prove to be
more troublesome in some implementations. This also will require a reference-within-reference
design in trait methods with a &self
receiver, and some more boilerplate if it needs to be cloned:
use implementation::Impl;
trait DoSomething {
fn something(&self);
}
impl<'t, T> DoSomething for Impl<&'t T>
where T: Clone + Send + 'static
{
// self is an `&Impl<&T>`:
fn something(&self) {
// it will require some more code to make a proper clone of T:
let t_clone = self.into_inner().clone();
let handle = std::thread::spawn(move || {
let implementation = Impl::new(&t_clone);
// Do something else with Impl<&T>
});
handle.join().unwrap();
}
}
Implementations§
Trait Implementations§
Source§impl<T: Ord> Ord for Impl<T>
impl<T: Ord> Ord for Impl<T>
Source§impl<T: PartialOrd> PartialOrd for Impl<T>
impl<T: PartialOrd> PartialOrd for Impl<T>
impl<T: Copy> Copy for Impl<T>
impl<T: Eq> Eq for Impl<T>
impl<T> StructuralPartialEq for Impl<T>
Auto Trait Implementations§
impl<T> Freeze for Impl<T>where
T: Freeze,
impl<T> RefUnwindSafe for Impl<T>where
T: RefUnwindSafe,
impl<T> Send for Impl<T>where
T: Send,
impl<T> Sync for Impl<T>where
T: Sync,
impl<T> Unpin for Impl<T>where
T: Unpin,
impl<T> UnwindSafe for Impl<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more