cxx_build/
vec.rs

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use crate::intern::{self, InternedString};
use std::path::Path;

pub(crate) trait InternedVec<T>
where
    T: ?Sized,
{
    fn vec(&self) -> Vec<&'static T>;
}

impl<T> InternedVec<T> for Vec<InternedString>
where
    T: ?Sized + Element,
{
    fn vec(&self) -> Vec<&'static T> {
        self.iter().copied().map(Element::unintern).collect()
    }
}

pub(crate) fn intern<T>(elements: &[&T]) -> Vec<InternedString>
where
    T: ?Sized + Element,
{
    elements.iter().copied().map(Element::intern).collect()
}

pub(crate) trait Element {
    fn intern(&self) -> InternedString;
    fn unintern(_: InternedString) -> &'static Self;
}

impl Element for str {
    fn intern(&self) -> InternedString {
        intern::intern(self)
    }

    fn unintern(interned: InternedString) -> &'static Self {
        interned.str()
    }
}

impl Element for Path {
    fn intern(&self) -> InternedString {
        intern::intern(&self.to_string_lossy())
    }

    fn unintern(interned: InternedString) -> &'static Self {
        Path::new(interned.str())
    }
}