Skip to main content

co_primitives/library/
is_default.rs

1// SPDX-License-Identifier: AGPL-3.0-only
2// Copyright (C) 2026 1io BRANDGUARDIAN GmbH
3
4/// Simple trait to check if the current value is the default.
5///
6/// This is particulary useful with serde:
7/// ```rust
8/// use serde::Serialize;
9/// use co_primitives::IsDefault;
10///
11/// #[derive(Debug, Serialize)]
12/// pub struct Hello {
13///    #[serde(default, skip_serializing_if = "IsDefault::is_default")]
14///    pub world: bool,
15/// }
16///
17/// assert_eq!(serde_json::to_string(&Hello { world: Default::default() }).unwrap(), "{}");
18/// ```
19pub trait IsDefault {
20	fn is_default(&self) -> bool;
21}
22impl<T> IsDefault for T
23where
24	T: Default + PartialEq,
25{
26	fn is_default(&self) -> bool {
27		&T::default() == self
28	}
29}