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", so this trait is not object safe.
Implementations on Foreign Types§
Source§impl<T: ?Sized> FragileTryMutContainer<T> for Box<T>
impl<T: ?Sized> FragileTryMutContainer<T> for Box<T>
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>>
impl<T: ?Sized> FragileTryMutContainer<T> for Rc<RefCell<T>>
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
Source§impl<T: ?Sized> FragileTryMutContainer<T> for Arc<Mutex<T>>
impl<T: ?Sized> FragileTryMutContainer<T> for Arc<Mutex<T>>
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>>
impl<T: ?Sized> FragileTryMutContainer<T> for Arc<RwLock<T>>
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.