use core::fmt;
#[derive(Clone, Copy, PartialEq, Eq, Default)]
#[repr(transparent)]
pub struct WireBool([u8; 1]);
const _: () = assert!(core::mem::size_of::<WireBool>() == 1);
const _: () = assert!(core::mem::align_of::<WireBool>() == 1);
impl WireBool {
pub const FALSE: Self = Self([0]);
pub const TRUE: Self = Self([1]);
#[inline(always)]
pub const fn new(v: bool) -> Self {
Self([v as u8])
}
#[inline(always)]
pub const fn get(self) -> bool {
self.0[0] != 0
}
#[inline(always)]
pub fn set(&mut self, v: bool) {
self.0[0] = v as u8;
}
}
impl From<bool> for WireBool {
#[inline(always)]
fn from(v: bool) -> Self {
Self::new(v)
}
}
impl From<WireBool> for bool {
#[inline(always)]
fn from(w: WireBool) -> Self {
w.get()
}
}
impl fmt::Debug for WireBool {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "WireBool({})", self.get())
}
}
impl fmt::Display for WireBool {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.get())
}
}
unsafe impl crate::abi::WireType for WireBool {
const WIRE_SIZE: usize = 1;
const CANONICAL_NAME: &'static str = "bool";
}
#[cfg(feature = "hopper-native-backend")]
unsafe impl ::hopper_runtime::__hopper_native::bytemuck::Zeroable for WireBool {}
#[cfg(feature = "hopper-native-backend")]
unsafe impl ::hopper_runtime::__hopper_native::bytemuck::Pod for WireBool {}
unsafe impl crate::account::Pod for WireBool {}
unsafe impl ::hopper_runtime::__sealed::HopperZeroCopySealed for WireBool {}
impl crate::account::FixedLayout for WireBool {
const SIZE: usize = 1;
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn bool_roundtrip() {
assert!(WireBool::new(true).get());
assert!(!WireBool::new(false).get());
}
#[test]
fn nonzero_is_true() {
let w = WireBool([0xFF]);
assert!(w.get());
}
}