netstack3_base/
convert.rs1pub trait BidirectionalConverter<Input, Output> {
14 fn convert(&self, a: Input) -> Output;
16
17 fn convert_back(&self, b: Output) -> Input;
19}
20
21impl<I> BidirectionalConverter<I, I> for () {
22 fn convert_back(&self, value: I) -> I {
23 value
24 }
25 fn convert(&self, value: I) -> I {
26 value
27 }
28}
29
30pub trait OwnedOrRefsBidirectionalConverter<Input, Output>:
32 BidirectionalConverter<Input, Output>
33 + for<'a> BidirectionalConverter<&'a Input, &'a Output>
34 + for<'a> BidirectionalConverter<&'a mut Input, &'a mut Output>
35{
36}
37
38impl<I, O, B> OwnedOrRefsBidirectionalConverter<I, O> for B where
39 B: BidirectionalConverter<I, O>
40 + for<'a> BidirectionalConverter<&'a I, &'a O>
41 + for<'a> BidirectionalConverter<&'a mut I, &'a mut O>
42{
43}
44
45#[cfg(test)]
46mod test {
47 use super::*;
48
49 #[test]
50 fn bidirectional_converter_identity() {
51 let a = ();
52 assert_eq!(a.convert(123), 123);
53 assert_eq!(a.convert_back(123), 123);
54 }
55}