netstack3_base/
test_only.rs

1// Copyright 2025 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.
4
5//! Abstractions enabling test-only behavior.
6
7pub use inner::{TestOnlyFrom, TestOnlyPartialEq};
8
9// The implementation for test code.
10#[cfg(any(test, feature = "testutils"))]
11mod inner {
12    pub use crate::Counter;
13
14    /// Applies `PartialEq` bounds, only in tests.
15    pub trait TestOnlyPartialEq: PartialEq {}
16
17    impl<T: PartialEq> TestOnlyPartialEq for T {}
18
19    // This implementation is necessary to satisfy trait bounds, but it's
20    // likely a programming error to try to use it.
21    impl PartialEq for Counter {
22        fn eq(&self, _other: &Self) -> bool {
23            panic!("The `Counter` type shouldn't be checked for equality")
24        }
25    }
26
27    /// Applies `From` bounds, only in tests.
28    pub trait TestOnlyFrom<T>: From<T> {}
29
30    impl<T1, T2: From<T1>> TestOnlyFrom<T1> for T2 {}
31}
32
33// The implementation for non-test code
34#[cfg(not(any(test, feature = "testutils")))]
35mod inner {
36
37    /// Applies `PartialEq` bounds, only in tests.
38    pub trait TestOnlyPartialEq {}
39
40    impl<T> TestOnlyPartialEq for T {}
41
42    /// Applies `From` bounds, only in tests.
43    pub trait TestOnlyFrom<T> {}
44
45    impl<T1, T2> TestOnlyFrom<T1> for T2 {}
46}