generic_container/impls/
box_container.rs

1use core::convert::Infallible;
2use alloc::boxed::Box;
3
4use crate::container_traits::{
5    Container, FragileContainer, FragileMutContainer, FragileTryContainer, FragileTryMutContainer,
6    MutContainer, TryContainer, TryMutContainer,
7};
8
9
10impl<T: ?Sized> FragileTryContainer<T> for Box<T> {
11    type Ref<'a>  = &'a T where T: 'a;
12    type RefError = Infallible;
13
14    #[inline]
15    fn new_container(t: T) -> Self where T: Sized {
16        Self::new(t)
17    }
18
19    /// Infallibly get the inner `T` of this box.
20    #[inline]
21    fn into_inner(self) -> Option<T> where T: Sized {
22        Some(*self)
23    }
24
25    /// Infallibly get immutable access to the inner `T`.
26    #[inline]
27    fn try_get_ref(&self) -> Result<Self::Ref<'_>, Self::RefError> {
28        Ok(self)
29    }
30}
31
32impl<T: ?Sized> TryContainer<T> for Box<T> {}
33
34impl<T: ?Sized> FragileContainer<T> for Box<T> {
35    /// Infallibly get immutable access to the inner `T`.
36    #[inline]
37    fn get_ref(&self) -> Self::Ref<'_> {
38        self
39    }
40}
41
42impl<T: ?Sized> Container<T> for Box<T> {}
43
44impl<T: ?Sized> FragileTryMutContainer<T> for Box<T> {
45    type RefMut<'a>  = &'a mut T where T: 'a;
46    type RefMutError = Infallible;
47
48    /// Infallibly get mutable access to the inner `T`.
49    #[inline]
50    fn try_get_mut(&mut self) -> Result<Self::RefMut<'_>, Self::RefMutError> {
51        Ok(self)
52    }
53}
54
55impl<T: ?Sized> TryMutContainer<T> for Box<T> {}
56
57impl<T: ?Sized> FragileMutContainer<T> for Box<T> {
58    /// Infallibly get mutable access to the inner `T`.
59    #[inline]
60    fn get_mut(&mut self) -> Self::RefMut<'_> {
61        self
62    }
63}
64
65impl<T: ?Sized> MutContainer<T> for Box<T> {}