assembly_core/
borrow.rs

1//! # Utilities for borrowing
2use std::borrow::{Borrow, BorrowMut};
3
4/// An enum that provides a mutable reference by either owning or
5/// borrowing a struct (Own or Mutable)
6pub enum Oom<'a, T> {
7    /// An owned reference to T
8    Own(T),
9    /// A mutable reference to T
10    Mut(&'a mut T),
11}
12
13impl<'a, T> AsMut<T> for Oom<'a, T> {
14    fn as_mut(&mut self) -> &mut T {
15        match self {
16            Oom::Own(data) => data,
17            Oom::Mut(data) => data,
18        }
19    }
20}
21
22impl<'a, T> AsRef<T> for Oom<'a, T> {
23    fn as_ref(&self) -> &T {
24        match self {
25            Oom::Own(data) => data,
26            Oom::Mut(data) => data,
27        }
28    }
29}
30
31impl<'a, T> Borrow<T> for Oom<'a, T> {
32    fn borrow(&self) -> &T {
33        self.as_ref()
34    }
35}
36
37impl<'a, T> BorrowMut<T> for Oom<'a, T> {
38    fn borrow_mut(&mut self) -> &mut T {
39        self.as_mut()
40    }
41}
42
43impl<'a, T> From<T> for Oom<'a, T> {
44    fn from(own: T) -> Self {
45        Oom::Own(own)
46    }
47}
48
49impl<'a, T> From<&'a mut T> for Oom<'a, T> {
50    fn from(some: &'a mut T) -> Self {
51        Oom::Mut(some)
52    }
53}