From 9133ef360714e6f99c754aecf3077fdf15fe8933 Mon Sep 17 00:00:00 2001 From: "josh@gitbutler.com" Date: Tue, 12 Dec 2023 17:25:15 +0100 Subject: [PATCH] add `DefaultTrue` type --- packages/tauri/src/lib.rs | 1 + packages/tauri/src/types.rs | 1 + packages/tauri/src/types/default_true.rs | 113 +++++++++++++++++++++++ 3 files changed, 115 insertions(+) create mode 100644 packages/tauri/src/types.rs create mode 100644 packages/tauri/src/types/default_true.rs diff --git a/packages/tauri/src/lib.rs b/packages/tauri/src/lib.rs index 265c914a0..0bcf3c14a 100644 --- a/packages/tauri/src/lib.rs +++ b/packages/tauri/src/lib.rs @@ -24,6 +24,7 @@ pub mod sentry; pub mod sessions; pub mod ssh; pub mod storage; +pub mod types; pub mod users; pub mod virtual_branches; pub mod watcher; diff --git a/packages/tauri/src/types.rs b/packages/tauri/src/types.rs new file mode 100644 index 000000000..3fa5f859e --- /dev/null +++ b/packages/tauri/src/types.rs @@ -0,0 +1 @@ +pub mod default_true; diff --git a/packages/tauri/src/types/default_true.rs b/packages/tauri/src/types/default_true.rs new file mode 100644 index 000000000..1e865eae6 --- /dev/null +++ b/packages/tauri/src/types/default_true.rs @@ -0,0 +1,113 @@ +#[derive(Clone, Copy, PartialEq, Eq, Hash)] +pub struct DefaultTrue(bool); + +impl core::fmt::Debug for DefaultTrue { + #[inline] + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + ::fmt(&self.0, f) + } +} + +impl core::fmt::Display for DefaultTrue { + #[inline] + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + ::fmt(&self.0, f) + } +} + +impl Default for DefaultTrue { + #[inline] + fn default() -> Self { + DefaultTrue(true) + } +} + +impl From for bool { + #[inline] + fn from(default_true: DefaultTrue) -> Self { + default_true.0 + } +} + +impl From for DefaultTrue { + #[inline] + fn from(boolean: bool) -> Self { + DefaultTrue(boolean) + } +} + +impl serde::Serialize for DefaultTrue { + #[inline] + fn serialize(&self, serializer: S) -> Result { + serializer.serialize_bool(self.0) + } +} + +impl<'de> serde::Deserialize<'de> for DefaultTrue { + #[inline] + fn deserialize>(deserializer: D) -> Result { + Ok(DefaultTrue(bool::deserialize(deserializer)?)) + } +} + +impl core::ops::Deref for DefaultTrue { + type Target = bool; + + #[inline] + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl core::ops::DerefMut for DefaultTrue { + #[inline] + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.0 + } +} + +impl PartialEq for DefaultTrue { + #[inline] + fn eq(&self, other: &bool) -> bool { + self.0 == *other + } +} + +impl PartialEq for bool { + #[inline] + fn eq(&self, other: &DefaultTrue) -> bool { + *self == other.0 + } +} + +impl core::ops::Not for DefaultTrue { + type Output = bool; + + #[inline] + fn not(self) -> Self::Output { + !self.0 + } +} + +#[cfg(test)] +mod tests { + use super::DefaultTrue; + + #[test] + #[allow(clippy::bool_assert_comparison)] + fn test_default_true() { + let default_true = DefaultTrue::default(); + assert!(default_true); + assert_eq!(default_true, true); + assert_eq!(!default_true, false); + assert!(!!default_true); + + if !(*default_true) { + unreachable!("default_true is false") + } + + let mut default_true = DefaultTrue::default(); + *default_true = false; + assert!(!default_true); + } +}