From 3da136a08ce5cb191e66159520aad7dd7a23d7bd Mon Sep 17 00:00:00 2001 From: crStiv Date: Wed, 25 Dec 2024 07:19:36 +0100 Subject: [PATCH] Optimize note_type_id to use 7 bits --- src/note_type.rs | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 src/note_type.rs diff --git a/src/note_type.rs b/src/note_type.rs new file mode 100644 index 00000000000..0c483ea1080 --- /dev/null +++ b/src/note_type.rs @@ -0,0 +1,47 @@ +pub struct NoteTypeId(u8); + +impl NoteTypeId { + // Constructor that checks if the value fits within 7 bits + pub fn new(id: u8) -> Result { + if id > 0x7F { // 0x7F = 0b01111111 (7 bits) + return Err("NoteTypeId must fit in 7 bits"); + } + Ok(Self(id)) + } + + // Serialization to bytes (1 byte instead of 32) + pub fn to_bytes(&self) -> [u8; 1] { + [self.0] + } + + // Deserialization from bytes + pub fn from_bytes(bytes: [u8; 1]) -> Result { + Self::new(bytes[0]) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_note_type_id_validation() { + // Valid values (7 bits) + assert!(NoteTypeId::new(0).is_ok()); + assert!(NoteTypeId::new(127).is_ok()); + + // Invalid values (>7 bits) + assert!(NoteTypeId::new(128).is_err()); + assert!(NoteTypeId::new(255).is_err()); + } + + #[test] + fn test_serialization() { + let id = NoteTypeId::new(42).unwrap(); + let bytes = id.to_bytes(); + assert_eq!(bytes.len(), 1); + + let decoded = NoteTypeId::from_bytes(bytes).unwrap(); + assert_eq!(decoded.0, 42); + } +}