1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
pub mod code_point;
pub mod code_source;
pub mod file_relative;
pub mod structure;

pub trait ConvertibleToRef<Output> {
    fn try_as_ref(&self) -> Option<&Output>;
}

pub trait ConvertibleToMut<Output>: ConvertibleToRef<Output> {
    fn try_as_mut(&mut self) -> Option<&mut Output>;
}

impl<T, U> ConvertibleToRef<U> for T
where
    for<'a> &'a U: TryFrom<&'a T>,
{
    fn try_as_ref(&self) -> Option<&U> {
        self.try_into().ok()
    }
}

impl<T, U> ConvertibleToMut<U> for T
where
    for<'a> &'a mut U: TryFrom<&'a mut T>,
    T: ConvertibleToRef<U>,
{
    fn try_as_mut(&mut self) -> Option<&mut U> {
        self.try_into().ok()
    }
}