1// Copyright 2022 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
45//! General utilities for converting types.
67/// Provides functions for converting infallibly between types.
8///
9/// This trait can be implemented on types that allow converting between two
10/// related types. It has two blanket implementations: `()` for identity
11/// conversions, i.e. `Input=Output`, and [`UninstantiableConverter`] as an
12/// uninstantiable type that implements the trait for any input and output.
13pub trait BidirectionalConverter<Input, Output> {
14/// Converts an instance of `Input` into an instance of `Output`.
15fn convert(&self, a: Input) -> Output;
1617/// Converts an instance of `Output` into an instance of `Input`.
18fn convert_back(&self, b: Output) -> Input;
19}
2021impl<I> BidirectionalConverter<I, I> for () {
22fn convert_back(&self, value: I) -> I {
23 value
24 }
25fn convert(&self, value: I) -> I {
26 value
27 }
28}
2930/// A marker trait for [`BidirectionalConverter`] of owned or reference types.
31pub 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}
3738impl<I, O, B> OwnedOrRefsBidirectionalConverter<I, O> for B where
39B: BidirectionalConverter<I, O>
40 + for<'a> BidirectionalConverter<&'a I, &'a O>
41 + for<'a> BidirectionalConverter<&'a mut I, &'a mut O>
42{
43}
4445#[cfg(test)]
46mod test {
47use super::*;
4849#[test]
50fn bidirectional_converter_identity() {
51let a = ();
52assert_eq!(a.convert(123), 123);
53assert_eq!(a.convert_back(123), 123);
54 }
55}