#![no_std]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![deny(
clippy::all,
clippy::missing_const_for_fn,
clippy::must_use_candidate,
clippy::ptr_as_ptr,
clippy::use_self,
missing_debug_implementations,
unused
)]
mod enums;
pub mod capsule;
pub mod firmware_storage;
pub mod protocol;
pub mod table;
pub mod time;
mod net;
mod status;
use core::cmp;
pub use net::*;
pub use status::Status;
pub use uguid::{Guid, guid};
use core::ffi::c_void;
use core::hash::{Hash, Hasher};
pub type Event = *mut c_void;
pub type Handle = *mut c_void;
pub type Char8 = u8;
pub type Char16 = u16;
pub type PhysicalAddress = u64;
pub type VirtualAddress = u64;
#[derive(Copy, Clone, Debug, Default)]
#[repr(transparent)]
pub struct Boolean(pub u8);
impl Boolean {
pub const TRUE: Self = Self(1);
pub const FALSE: Self = Self(0);
}
impl From<u8> for Boolean {
fn from(value: u8) -> Self {
Self(value)
}
}
impl From<bool> for Boolean {
fn from(value: bool) -> Self {
match value {
true => Self(1),
false => Self(0),
}
}
}
impl From<Boolean> for bool {
#[expect(clippy::match_like_matches_macro)]
fn from(value: Boolean) -> Self {
match value.0 {
0 => false,
_ => true,
}
}
}
impl PartialEq for Boolean {
fn eq(&self, other: &Self) -> bool {
match (self.0, other.0) {
(0, 0) => true,
(0, _) => false,
(_, 0) => false,
(_, _) => true,
}
}
}
impl Eq for Boolean {}
impl PartialOrd for Boolean {
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
Some(self.cmp(other))
}
}
impl Ord for Boolean {
fn cmp(&self, other: &Self) -> cmp::Ordering {
match (self.0, other.0) {
(0, 0) => cmp::Ordering::Equal,
(0, _) => cmp::Ordering::Less,
(_, 0) => cmp::Ordering::Greater,
(_, _) => cmp::Ordering::Equal,
}
}
}
impl Hash for Boolean {
fn hash<H: Hasher>(&self, state: &mut H) {
let seed = if self.0 == 0 { 0 } else { 1 };
state.write_u8(seed);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_boolean_abi() {
assert_eq!(size_of::<Boolean>(), 1);
assert_eq!(Boolean::from(true).0, 1);
assert_eq!(Boolean::from(false).0, 0);
assert_eq!(Boolean::TRUE.0, 1);
assert_eq!(Boolean::FALSE.0, 0);
assert!(!bool::from(Boolean(0b0)));
assert!(bool::from(Boolean(0b1)));
assert!(bool::from(Boolean(0b11111110)));
assert!(bool::from(Boolean(0b11111111)));
}
#[test]
fn test_order() {
assert!(Boolean::FALSE < Boolean::TRUE);
assert!(Boolean::TRUE > Boolean::FALSE);
}
#[test]
fn test_equality() {
assert_eq!(Boolean(0), Boolean(0));
assert_ne!(Boolean(0), Boolean(3));
assert_ne!(Boolean(7), Boolean(0));
assert_eq!(Boolean(1), Boolean(1));
assert_eq!(Boolean(13), Boolean(7));
}
#[test]
fn test_hash() {
#[derive(Default)]
struct TestHasher(u64);
impl Hasher for TestHasher {
fn finish(&self) -> u64 {
self.0
}
fn write(&mut self, bytes: &[u8]) {
for byte in bytes {
self.0 = self.0.wrapping_mul(257).wrapping_add(u64::from(*byte));
}
}
}
fn calculate_hash(value: Boolean) -> u64 {
let mut hasher = TestHasher::default();
value.hash(&mut hasher);
hasher.finish()
}
assert_eq!(calculate_hash(Boolean(1)), calculate_hash(Boolean(13)));
assert_eq!(calculate_hash(Boolean(13)), calculate_hash(Boolean(255)));
assert_ne!(calculate_hash(Boolean(0)), calculate_hash(Boolean(13)));
}
}