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
32
33
34
35
36
#![allow(clippy::needless_lifetimes)]

use std::any::type_name;

use crate::graph::utils::{FromOptVec, OptVec};

#[repr(transparent)]
pub struct Identity<T>(pub T);

impl<T> FromOptVec for Identity<T> {
    type Ref<'a> = &'a T where Self::T: 'a;
    type Mut<'a> = &'a mut T where Self::T: 'a;
    type T = T;

    fn unwrap<'a>(value: OptVec<&'a Self::T>) -> Self::Ref<'a> {
        match value {
            OptVec::Single(x) => x,
            _ => panic!(
                "Container must has only one child <{}>, but has {}",
                type_name::<T>(),
                value.len()
            ),
        }
    }

    fn unwrap_mut<'a>(value: OptVec<&'a mut Self::T>) -> Self::Mut<'a> {
        match value {
            OptVec::Single(x) => x,
            _ => panic!(
                "Container must has only one child <{}>, but has {}",
                type_name::<T>(),
                value.len()
            ),
        }
    }
}