hugr_model/v0/table/
view.rs

1use super::Module;
2use std::sync::Arc;
3
4/// Trait for views into a [`Module`].
5pub trait View<'a, S>: Sized {
6    /// Attempt to interpret a subpart of a module as this type.
7    fn view(module: &'a Module<'a>, src: S) -> Option<Self>;
8}
9
10impl<'a, S, T> View<'a, &S> for T
11where
12    T: View<'a, S>,
13    S: Copy,
14{
15    fn view(module: &'a Module<'a>, id: &S) -> Option<Self> {
16        T::view(module, *id)
17    }
18}
19
20impl<'a, S, T> View<'a, Option<S>> for Option<T>
21where
22    T: View<'a, S>,
23{
24    fn view(module: &'a Module<'a>, src: Option<S>) -> Option<Self> {
25        Some(match src {
26            Some(src) => Some(T::view(module, src)?),
27            None => None,
28        })
29    }
30}
31
32impl<'a, S, T> View<'a, &'a [S]> for Vec<T>
33where
34    T: View<'a, &'a S>,
35{
36    fn view(module: &'a Module<'a>, sources: &'a [S]) -> Option<Self> {
37        sources
38            .iter()
39            .map(|source| T::view(module, source))
40            .collect()
41    }
42}
43
44impl<'a, S, T> View<'a, &'a [S]> for Box<[T]>
45where
46    T: View<'a, &'a S>,
47{
48    fn view(module: &'a Module<'a>, sources: &'a [S]) -> Option<Self> {
49        sources
50            .iter()
51            .map(|source| T::view(module, source))
52            .collect()
53    }
54}
55
56impl<'a, S, T> View<'a, &'a [S]> for Arc<[T]>
57where
58    T: View<'a, &'a S>,
59{
60    fn view(module: &'a Module<'a>, sources: &'a [S]) -> Option<Self> {
61        sources
62            .iter()
63            .map(|source| T::view(module, source))
64            .collect()
65    }
66}