#[repr(transparent)]pub struct GenericContainer<T: ?Sized, C: ?Sized> {
pub _marker: PhantomData<T>,
pub container: C,
}Expand description
A wrapper type intended for use in blanket implementations of YourTrait ranging over
containers C that hold a T: YourTrait.
This is necessary to avoid conflicting trait implementations.
§Examples
Not needed when the T: YourTrait is fixed:
use generic_container::Container;
trait Trait {
fn do_thing(&self);
}
impl<C: ?Sized + Container<dyn Trait>> Trait for C {
fn do_thing(&self) {
self.get_ref().do_thing();
}
}But when the inner type varies:
use generic_container::{Container, GenericContainer};
trait Trait {
fn do_thing(&self);
}
impl<T: ?Sized + Trait, C: ?Sized + Container<T>> Trait for GenericContainer<T, C> {
fn do_thing(&self) {
self.container.get_ref().do_thing();
}
}Without a wrapper type, it does not compile:
ⓘ
use generic_container::{Container, GenericContainer};
trait Trait {
fn do_thing(&self);
}
impl<T: ?Sized + Trait, C: ?Sized + Container<T>> Trait for C {
fn do_thing(&self) {
self.get_ref().do_thing();
}
}Fields§
§_marker: PhantomData<T>Distinguish which type is supposed to be contained.
container: CShould implement the base container trait, FragileTryContainer<T>.
Implementations§
Trait Implementations§
Source§impl<T, C> Debug for GenericContainer<T, C>
impl<T, C> Debug for GenericContainer<T, C>
Source§impl<T: ?Sized, C: ?Sized + Ord> Ord for GenericContainer<T, C>
impl<T: ?Sized, C: ?Sized + Ord> Ord for GenericContainer<T, C>
Source§impl<T: ?Sized, C: ?Sized + PartialOrd<C>> PartialOrd for GenericContainer<T, C>
impl<T: ?Sized, C: ?Sized + PartialOrd<C>> PartialOrd for GenericContainer<T, C>
impl<T: ?Sized, C: Copy> Copy for GenericContainer<T, C>
impl<T: ?Sized, C: ?Sized + Eq> Eq for GenericContainer<T, C>
Auto Trait Implementations§
impl<T, C> Freeze for GenericContainer<T, C>
impl<T, C> RefUnwindSafe for GenericContainer<T, C>
impl<T, C> Send for GenericContainer<T, C>
impl<T, C> Sync for GenericContainer<T, C>
impl<T, C> Unpin for GenericContainer<T, C>
impl<T, C> UnwindSafe for GenericContainer<T, C>
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> FragileContainer<T> for Twhere
T: ?Sized,
impl<T> FragileContainer<T> for Twhere
T: ?Sized,
Source§fn get_ref(&self) -> <T as FragileTryContainer<T>>::Ref<'_>
fn get_ref(&self) -> <T as FragileTryContainer<T>>::Ref<'_>
Infallibly get immutable access to the T.
Source§impl<T> FragileMutContainer<T> for Twhere
T: ?Sized,
impl<T> FragileMutContainer<T> for Twhere
T: ?Sized,
Source§fn get_mut(&mut self) -> <T as FragileTryMutContainer<T>>::RefMut<'_>
fn get_mut(&mut self) -> <T as FragileTryMutContainer<T>>::RefMut<'_>
Infallibly get mutable access to the T.
Source§impl<T> FragileTryContainer<T> for Twhere
T: ?Sized,
impl<T> FragileTryContainer<T> for Twhere
T: ?Sized,
Source§fn into_inner(self) -> Option<T>
fn into_inner(self) -> Option<T>
Infallibly get the T.
Source§fn try_get_ref(
&self,
) -> Result<<T as FragileTryContainer<T>>::Ref<'_>, <T as FragileTryContainer<T>>::RefError>
fn try_get_ref( &self, ) -> Result<<T as FragileTryContainer<T>>::Ref<'_>, <T as FragileTryContainer<T>>::RefError>
Infallibly get immutable access to the T.
Source§type RefError = Infallible
type RefError = Infallible
An error that might be returned by
try_get_ref. This type should implement
std::error::Error. Read moreSource§fn new_container(t: T) -> T
fn new_container(t: T) -> T
Create a new container that owns the provided
T.Source§impl<T> FragileTryMutContainer<T> for Twhere
T: ?Sized,
impl<T> FragileTryMutContainer<T> for Twhere
T: ?Sized,
Source§fn try_get_mut(
&mut self,
) -> Result<<T as FragileTryMutContainer<T>>::RefMut<'_>, <T as FragileTryMutContainer<T>>::RefMutError>
fn try_get_mut( &mut self, ) -> Result<<T as FragileTryMutContainer<T>>::RefMut<'_>, <T as FragileTryMutContainer<T>>::RefMutError>
Infallibly get mutable access to the T.
Source§type RefMut<'a> = &'a mut T
where
T: 'a
type RefMut<'a> = &'a mut T where T: 'a
A mutably borrowed value from the container. Read more
Source§type RefMutError = Infallible
type RefMutError = Infallible
An error that might be returned by
try_get_mut. This type should implement
std::error::Error. Read more