snarkvm_circuit_environment/traits/
inject.rs1use crate::Mode;
17
18pub trait Inject {
20    type Primitive;
21
22    fn new(mode: Mode, value: Self::Primitive) -> Self;
26
27    fn constant(value: Self::Primitive) -> Self
31    where
32        Self: Sized,
33    {
34        Self::new(Mode::Constant, value)
35    }
36}
37
38impl<C0: Eq + core::hash::Hash + Inject<Primitive = P0>, C1: Inject<Primitive = P1>, P0, P1> Inject
43    for indexmap::IndexMap<C0, C1>
44{
45    type Primitive = indexmap::IndexMap<P0, P1>;
46
47    #[inline]
48    fn new(mode: Mode, value: Self::Primitive) -> Self {
49        value.into_iter().map(|(v0, v1)| (C0::new(mode, v0), C1::new(mode, v1))).collect()
50    }
51}
52
53impl<C: Inject<Primitive = P>, P> Inject for Vec<C> {
58    type Primitive = Vec<P>;
59
60    #[inline]
61    fn new(mode: Mode, value: Self::Primitive) -> Self {
62        value.into_iter().map(|v| C::new(mode, v)).collect()
63    }
64}
65
66macro_rules! inject_tuple {
72    (($t0:ident, 0), $(($ty:ident, $idx:tt)),*) => {
73        impl<$t0: Inject, $($ty: Inject),*> Inject for ($t0, $($ty),*) {
74            type Primitive = ($t0::Primitive, $( $ty::Primitive ),*);
75
76            #[inline]
77            fn new(mode: Mode, value: Self::Primitive) -> Self {
78                ($t0::new(mode, value.0), $($ty::new(mode, value.$idx)),*)
79            }
80        }
81    }
82}
83
84inject_tuple!((C0, 0), (C1, 1));
85inject_tuple!((C0, 0), (C1, 1), (C2, 2));
86inject_tuple!((C0, 0), (C1, 1), (C2, 2), (C3, 3));
87inject_tuple!((C0, 0), (C1, 1), (C2, 2), (C3, 3), (C4, 4));