pub trait FragileTryMutContainer<T: ?Sized>: FragileTryContainer<T> {
type RefMut<'a>: DerefMut<Target = T>
where Self: 'a;
type RefMutError;
// Required method
fn try_get_mut(&mut self) -> Result<Self::RefMut<'_>, Self::RefMutError>;
}Expand description
An abstraction over some container which owns a T and can provide mutable or immutable
references to it, or be consumed to return the inner T (if T is Sized).
This is the base mutable container trait, which places the fewest requirements on container
implementations that can provide mutable access to the inner T.
§Fragility: Potential Panics or Deadlocks
Unless a FragileTryMutContainer is known to also implement TryMutContainer, it should be
treated as fragile.
§Errors
The into_inner, try_get_ref, and try_get_mut methods may be able to fail, depending
on the container; a container should clearly document the circumstances in which a None or
Err variant may be returned.
Required Associated Types§
Sourcetype RefMutError
type RefMutError
An error that might be returned by try_get_mut. This type should implement
std::error::Error.
The canonical error to use when try_get_mut can never return an error
is Infallible.
Required Methods§
Sourcefn try_get_mut(&mut self) -> Result<Self::RefMut<'_>, Self::RefMutError>
fn try_get_mut(&mut self) -> Result<Self::RefMut<'_>, Self::RefMutError>
Attempt to mutably access the inner T.
There are no particular constraints imposed on implementations. In particular, depending on the container implementation:
- the function could be infallible,
- the function could panic or deadlock (see below),
- retrying the function in a loop might never succeed.
However, if the container implements FragileMutContainer<T>, then implementors should
usually make try_get_mut infallible as well, unless there is some useful reason to not
do so.
§Fragility: Potential Panics or Deadlocks
Unless this FragileTryMutContainer is also a TryMutContainer, implementations are
permitted to panic or deadlock if this method is called from a thread which already has a
reference to the inner T of this container.
§Errors
Errors are implementation-defined, and should be documented by implementors.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".
Implementations on Foreign Types§
Source§impl<T: ?Sized> FragileTryMutContainer<T> for Arc<Mutex<T>>
Available on crate features std only.
impl<T: ?Sized> FragileTryMutContainer<T> for Arc<Mutex<T>>
std only.Source§fn try_get_mut(&mut self) -> Result<Self::RefMut<'_>, Self::RefMutError>
fn try_get_mut(&mut self) -> Result<Self::RefMut<'_>, Self::RefMutError>
Get mutable access to the inner T.
Uses Mutex::lock, so this container is
fragile.
§Panics and Deadlocks
Panics if a poison error is encountered, which can only occur if another thread has already panicked.
May also panic or deadlock if the contract of a fragile container is broken.
type RefMut<'a> = MutexGuard<'a, T> where T: 'a
type RefMutError = Infallible
Source§impl<T: ?Sized> FragileTryMutContainer<T> for Arc<RwLock<T>>
Available on crate features std only.
impl<T: ?Sized> FragileTryMutContainer<T> for Arc<RwLock<T>>
std only.Source§fn try_get_mut(&mut self) -> Result<Self::RefMut<'_>, Self::RefMutError>
fn try_get_mut(&mut self) -> Result<Self::RefMut<'_>, Self::RefMutError>
Get mutable access to the inner T.
Uses RwLock::write, so this container is
fragile.
§Panics and Deadlocks
Panics if a poison error is encountered, which can only occur if another thread has already panicked.
May also panic or deadlock if the contract of a fragile container is broken.
type RefMut<'a> = RwLockWriteGuard<'a, T> where T: 'a
type RefMutError = Infallible
Source§impl<T: ?Sized> FragileTryMutContainer<T> for Arc<ThreadCheckedMutex<T>>
Available on crate feature thread-checked-lock only.
impl<T: ?Sized> FragileTryMutContainer<T> for Arc<ThreadCheckedMutex<T>>
thread-checked-lock only.Source§fn try_get_mut(&mut self) -> Result<Self::RefMut<'_>, Self::RefMutError>
fn try_get_mut(&mut self) -> Result<Self::RefMut<'_>, Self::RefMutError>
Attempt to mutably access the inner T.
§Errors
This function fails if and only if ThreadCheckedMutex::lock fails.
A poison error is not ignored, nor does it trigger a panic.
type RefMut<'a> = ThreadCheckedMutexGuard<'a, T> where T: 'a
type RefMutError = ErasedLockError
Source§impl<T: ?Sized> FragileTryMutContainer<T> for Box<T>
Available on crate features alloc only.
impl<T: ?Sized> FragileTryMutContainer<T> for Box<T>
alloc only.Source§fn try_get_mut(&mut self) -> Result<Self::RefMut<'_>, Self::RefMutError>
fn try_get_mut(&mut self) -> Result<Self::RefMut<'_>, Self::RefMutError>
Infallibly get mutable access to the inner T.
type RefMut<'a> = &'a mut T where T: 'a
type RefMutError = Infallible
Source§impl<T: ?Sized> FragileTryMutContainer<T> for Rc<RefCell<T>>
Available on crate features alloc only.
impl<T: ?Sized> FragileTryMutContainer<T> for Rc<RefCell<T>>
alloc only.Source§fn try_get_mut(&mut self) -> Result<Self::RefMut<'_>, Self::RefMutError>
fn try_get_mut(&mut self) -> Result<Self::RefMut<'_>, Self::RefMutError>
Get mutable access to the inner T.
Uses RefCell::borrow_mut, so this container is
fragile.
§Panics
Panics if the contract of a fragile container is broken.
type RefMut<'a> = RefMut<'a, T> where T: 'a
type RefMutError = Infallible
Implementors§
Source§impl<T: ?Sized> FragileTryMutContainer<T> for CheckedRcRefCell<T>
Available on crate features alloc only.
impl<T: ?Sized> FragileTryMutContainer<T> for CheckedRcRefCell<T>
alloc only.