Skip to main content

neo_types/
boolean.rs

1// Copyright (c) 2025-2026 R3E Network
2// Licensed under the MIT License
3
4use std::fmt;
5use std::ops::{BitAnd, BitOr, BitXor, Not};
6
7#[cfg(feature = "serde")]
8use serde::{Deserialize, Serialize};
9
10/// Neo N3 Boolean type
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
12#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
13#[repr(transparent)]
14pub struct NeoBoolean(bool);
15
16impl NeoBoolean {
17    pub const TRUE: Self = Self(true);
18    pub const FALSE: Self = Self(false);
19
20    pub fn new(value: bool) -> Self {
21        Self(value)
22    }
23
24    pub fn as_bool(self) -> bool {
25        self.0
26    }
27}
28
29impl fmt::Display for NeoBoolean {
30    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31        write!(f, "{}", self.0)
32    }
33}
34
35impl From<bool> for NeoBoolean {
36    fn from(value: bool) -> Self {
37        Self(value)
38    }
39}
40
41impl From<NeoBoolean> for bool {
42    fn from(value: NeoBoolean) -> Self {
43        value.0
44    }
45}
46
47impl BitAnd for NeoBoolean {
48    type Output = Self;
49    fn bitand(self, rhs: Self) -> Self::Output {
50        Self(self.0 & rhs.0)
51    }
52}
53
54impl BitOr for NeoBoolean {
55    type Output = Self;
56    fn bitor(self, rhs: Self) -> Self::Output {
57        Self(self.0 | rhs.0)
58    }
59}
60
61impl BitXor for NeoBoolean {
62    type Output = Self;
63    fn bitxor(self, rhs: Self) -> Self::Output {
64        Self(self.0 ^ rhs.0)
65    }
66}
67
68impl Not for NeoBoolean {
69    type Output = Self;
70    fn not(self) -> Self::Output {
71        Self(!self.0)
72    }
73}
74
75impl Default for NeoBoolean {
76    fn default() -> Self {
77        Self::FALSE
78    }
79}