1
Fork 0
mirror of https://github.com/redstrate/Physis.git synced 2025-04-25 13:57:45 +00:00
physis/src/common_file_operations.rs

36 lines
794 B
Rust
Raw Normal View History

// SPDX-FileCopyrightText: 2024 Joshua Goins <josh@redstrate.com>
// SPDX-License-Identifier: GPL-3.0-or-later
pub(crate) fn read_bool_from<T: std::convert::From<u8> + std::cmp::PartialEq>(x: T) -> bool {
x == T::from(1u8)
}
pub(crate) fn write_bool_as<T: std::convert::From<u8>>(x: &bool) -> T {
2024-04-20 13:18:03 -04:00
if *x {
T::from(1u8)
} else {
T::from(0u8)
}
}
#[cfg(test)]
mod tests {
2024-04-16 21:19:08 -04:00
use super::*;
const DATA: [u8; 2] = [0u8, 1u8];
// TODO: add tests for u16
2024-04-16 21:19:08 -04:00
#[test]
fn read_bool_u8() {
assert!(!read_bool_from::<u8>(DATA[0]));
assert!(read_bool_from::<u8>(DATA[1]));
}
#[test]
fn write_bool_u8() {
assert_eq!(write_bool_as::<u8>(&false), DATA[0]);
assert_eq!(write_bool_as::<u8>(&true), DATA[1]);
}
2024-04-20 13:18:03 -04:00
}