aboutsummaryrefslogtreecommitdiff
path: root/src/primitive/bufferid.rs
diff options
context:
space:
mode:
authorTobias Deiminger <tdmg@linutronix.de>2024-04-16 22:06:31 +0200
committerMax Audron <audron@cocaine.farm>2025-02-25 00:10:39 +0100
commit14bce9f885ecfb4a0309a7aa398db033f1cc542e (patch)
tree8423c7c46f9b062c37e44c6edd0c8416611fff5a /src/primitive/bufferid.rs
parentUse MsgId in Message (diff)
Add BufferId as Rust type
Up to now it was represented as i32. If we introduce a newtype for it, we can handle it idiomatically as dedicated Variant::BufferId variant (instead of having it mashed into Variant::UserType).
Diffstat (limited to 'src/primitive/bufferid.rs')
-rw-r--r--src/primitive/bufferid.rs37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/primitive/bufferid.rs b/src/primitive/bufferid.rs
new file mode 100644
index 0000000..6ee2447
--- /dev/null
+++ b/src/primitive/bufferid.rs
@@ -0,0 +1,37 @@
+#[derive(Copy, Clone, Debug, std::cmp::PartialEq)]
+pub struct BufferId(pub i32);
+
+use crate::{deserialize::*, error::ProtocolError, serialize::*};
+
+impl Serialize for BufferId {
+ fn serialize(&self) -> Result<Vec<u8>, ProtocolError> {
+ self.0.serialize()
+ }
+}
+
+impl Deserialize for BufferId {
+ fn parse(b: &[u8]) -> Result<(usize, Self), ProtocolError> {
+ let (size, value) = i32::parse(b)?;
+ return Ok((size, BufferId(value)));
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ pub fn bufferid_parse_test() {
+ let test_bytes: &[u8] = &[0, 0, 0, 1];
+ let (len, res) = BufferId::parse(test_bytes).unwrap();
+ assert_eq!(len, test_bytes.len());
+ assert_eq!(res, BufferId(1));
+ }
+
+ #[test]
+ pub fn bufferid_serialize_test() {
+ let res = BufferId(1).serialize().unwrap();
+ let expected_bytes: &[u8] = &[0, 0, 0, 1];
+ assert_eq!(res, expected_bytes);
+ }
+}