Skip to main content

redis_module/
io.rs

1use crate::error::Error;
2use crate::raw;
3use crate::RedisBuffer;
4
5/// Wrapper around RedisModuleIO for type-safe serialization/deserialization.
6///
7/// This wrapper provides an ergonomic API for working with RedisModuleIO
8/// RDB serialization operations while maintaining compatibility with the underlying C bindings.
9pub struct RedisModuleIO {
10    io: *mut raw::RedisModuleIO,
11}
12
13impl RedisModuleIO {
14    /// Creates a new RedisModuleIO wrapper from a raw pointer.
15    pub fn new(io: *mut raw::RedisModuleIO) -> Self {
16        Self { io }
17    }
18
19    /// Returns the raw RedisModuleIO pointer.
20    pub fn as_ptr(&self) -> *mut raw::RedisModuleIO {
21        self.io
22    }
23
24    /// Reads a string from the IO stream.
25    ///
26    /// # Errors
27    /// Returns an error if the operation fails or if an IO error occurred.
28    pub fn read_string(&mut self) -> Result<String, Error> {
29        let string_ptr = raw::load_string(self.io)?;
30        Ok(string_ptr.to_string_lossy())
31    }
32
33    /// Writes a string to the IO stream.
34    pub fn write_string(&mut self, s: &str) {
35        raw::save_string(self.io, s);
36    }
37
38    /// Reads an unsigned integer from the IO stream.
39    ///
40    /// # Errors
41    /// Returns an error if the operation fails or if an IO error occurred.
42    pub fn read_unsigned(&mut self) -> Result<u64, Error> {
43        raw::load_unsigned(self.io)
44    }
45
46    /// Writes an unsigned integer to the IO stream.
47    pub fn write_unsigned(&mut self, val: u64) {
48        raw::save_unsigned(self.io, val);
49    }
50
51    /// Reads a signed integer from the IO stream.
52    ///
53    /// # Errors
54    /// Returns an error if the operation fails or if an IO error occurred.
55    pub fn read_signed(&mut self) -> Result<i64, Error> {
56        raw::load_signed(self.io)
57    }
58
59    /// Writes a signed integer to the IO stream.
60    pub fn write_signed(&mut self, val: i64) {
61        raw::save_signed(self.io, val);
62    }
63
64    /// Reads a double from the IO stream.
65    ///
66    /// # Errors
67    /// Returns an error if the operation fails or if an IO error occurred.
68    pub fn read_double(&mut self) -> Result<f64, Error> {
69        raw::load_double(self.io)
70    }
71
72    /// Writes a double to the IO stream.
73    pub fn write_double(&mut self, val: f64) {
74        raw::save_double(self.io, val);
75    }
76
77    /// Reads a float from the IO stream.
78    ///
79    /// # Errors
80    /// Returns an error if the operation fails or if an IO error occurred.
81    pub fn read_float(&mut self) -> Result<f32, Error> {
82        raw::load_float(self.io)
83    }
84
85    /// Writes a float to the IO stream.
86    pub fn write_float(&mut self, val: f32) {
87        raw::save_float(self.io, val);
88    }
89
90    /// Reads a string buffer from the IO stream.
91    ///
92    /// # Errors
93    /// Returns an error if the operation fails or if an IO error occurred.
94    pub fn read_string_buffer(&mut self) -> Result<RedisBuffer, Error> {
95        raw::load_string_buffer(self.io)
96    }
97
98    /// Writes a slice to the IO stream.
99    pub fn write_slice(&mut self, buf: &[u8]) {
100        raw::save_slice(self.io, buf);
101    }
102}