|
1 | | -use std::{ffi::CString, marker::PhantomData, os::fd::RawFd, path::Path, ptr}; |
| 1 | +use std::{ |
| 2 | + ffi::CString, |
| 3 | + marker::PhantomData, |
| 4 | + os::fd::{IntoRawFd, OwnedFd}, |
| 5 | + path::Path, |
| 6 | + ptr, |
| 7 | +}; |
2 | 8 |
|
3 | 9 | use librdb_sys::{ |
4 | 10 | self, RdbHandlersDataCallbacks, RdbStatus_RDB_STATUS_ERROR, RdbStatus_RDB_STATUS_OK, |
@@ -116,27 +122,26 @@ impl<H: RdbHandlers> Parser<H> { |
116 | 122 | self.run_parse() |
117 | 123 | } |
118 | 124 |
|
119 | | - /// Attach a file-descriptor reader and parse the RDB data. |
| 125 | + /// Parse RDB data from a file descriptor. |
120 | 126 | /// |
121 | | - /// If `close_when_done` is true, librdb will close the fd after parsing. |
122 | | - /// The fd must be in blocking mode. |
| 127 | + /// Takes ownership of the fd. The fd must be in blocking mode. |
123 | 128 | /// |
124 | 129 | /// # Errors |
125 | 130 | /// Returns an error if the reader cannot be created, the RDB data is |
126 | 131 | /// malformed, or a handler callback returns `Err`. |
127 | | - pub fn parse_fd(&mut self, fd: RawFd, close_when_done: bool) -> Result<()> { |
| 132 | + pub fn parse_fd(&mut self, fd: OwnedFd) -> Result<()> { |
128 | 133 | if self.parsed { |
129 | 134 | return Err(RdbError::Parser { |
130 | 135 | code: 0, |
131 | 136 | message: "parser already used; create a new Parser instance".into(), |
132 | 137 | }); |
133 | 138 | } |
134 | 139 |
|
135 | | - // SAFETY: self.raw is a valid parser in CONFIGURING state. The caller |
136 | | - // is responsible for providing a valid, blocking fd. |
137 | | - let reader = unsafe { |
138 | | - librdb_sys::RDBX_createReaderFileDesc(self.raw, fd, i32::from(close_when_done)) |
139 | | - }; |
| 140 | + // SAFETY: self.raw is a valid parser in CONFIGURING state. |
| 141 | + // OwnedFd guarantees a valid fd; into_raw_fd transfers ownership to |
| 142 | + // librdb which will close it (fdCloseWhenDone = 1). |
| 143 | + let raw_fd = fd.into_raw_fd(); |
| 144 | + let reader = unsafe { librdb_sys::RDBX_createReaderFileDesc(self.raw, raw_fd, 1) }; |
140 | 145 | if reader.is_null() { |
141 | 146 | return Err(self.extract_c_error("RDBX_createReaderFileDesc returned null")); |
142 | 147 | } |
|
0 commit comments