Skip to main content

procwire_client/codec/
raw.rs

1//! Raw codec - pass-through for binary data.
2//!
3//! Used when payload is already serialized or is raw bytes.
4//! Provides zero-copy operations where possible.
5//!
6//! # Example
7//!
8//! ```
9//! use procwire_client::codec::RawCodec;
10//! use bytes::Bytes;
11//!
12//! // Serialize raw bytes
13//! let data = b"binary payload";
14//! let serialized = RawCodec::serialize(data);
15//! assert_eq!(&serialized[..], data);
16//!
17//! // Zero-copy with Bytes
18//! let bytes = Bytes::from_static(b"zero copy");
19//! let passed = RawCodec::serialize_bytes(bytes.clone());
20//! assert_eq!(passed.as_ptr(), bytes.as_ptr()); // Same memory
21//! ```
22
23use bytes::Bytes;
24
25/// Raw codec that passes bytes through without transformation.
26///
27/// This is the simplest codec - it does not perform any serialization.
28/// Use this when you have raw binary data that should be sent as-is.
29pub struct RawCodec;
30
31impl RawCodec {
32    /// Serialize raw bytes (copies data into Bytes).
33    ///
34    /// For truly zero-copy, use `serialize_bytes` with an existing `Bytes` value.
35    #[inline]
36    pub fn serialize(data: &[u8]) -> Bytes {
37        Bytes::copy_from_slice(data)
38    }
39
40    /// Serialize Bytes (true zero-copy, just returns the input).
41    #[inline]
42    pub fn serialize_bytes(data: Bytes) -> Bytes {
43        data
44    }
45
46    /// Deserialize - returns a reference to the input (zero-copy).
47    #[inline]
48    pub fn deserialize(data: &[u8]) -> &[u8] {
49        data
50    }
51
52    /// Deserialize Bytes - returns a reference to the data (zero-copy).
53    #[inline]
54    pub fn deserialize_bytes(data: &Bytes) -> &[u8] {
55        data
56    }
57}
58
59#[cfg(test)]
60mod tests {
61    use super::*;
62
63    #[test]
64    fn test_serialize_round_trip() {
65        let original = b"hello world";
66        let serialized = RawCodec::serialize(original);
67        let deserialized = RawCodec::deserialize(&serialized);
68        assert_eq!(deserialized, original);
69    }
70
71    #[test]
72    fn test_serialize_empty() {
73        let empty: &[u8] = b"";
74        let serialized = RawCodec::serialize(empty);
75        assert!(serialized.is_empty());
76        assert!(RawCodec::deserialize(&serialized).is_empty());
77    }
78
79    #[test]
80    fn test_serialize_large_buffer() {
81        let large = vec![0xAB; 1024 * 1024]; // 1MB
82        let serialized = RawCodec::serialize(&large);
83        assert_eq!(serialized.len(), 1024 * 1024);
84        assert_eq!(RawCodec::deserialize(&serialized), &large[..]);
85    }
86
87    #[test]
88    fn test_serialize_bytes_zero_copy() {
89        let original = Bytes::from_static(b"static data");
90        let serialized = RawCodec::serialize_bytes(original.clone());
91
92        // Should be the exact same Bytes instance
93        assert_eq!(serialized.as_ptr(), original.as_ptr());
94        assert_eq!(serialized.len(), original.len());
95    }
96
97    #[test]
98    fn test_deserialize_bytes_zero_copy() {
99        let bytes = Bytes::from_static(b"test data");
100        let deserialized = RawCodec::deserialize_bytes(&bytes);
101
102        // Should point to the same memory
103        assert_eq!(deserialized.as_ptr(), bytes.as_ptr());
104    }
105
106    #[test]
107    fn test_binary_data_preserved() {
108        // Test that all byte values are preserved
109        let all_bytes: Vec<u8> = (0..=255).collect();
110        let serialized = RawCodec::serialize(&all_bytes);
111        assert_eq!(RawCodec::deserialize(&serialized), &all_bytes[..]);
112    }
113}