[libvirt] [PATCH 2/2] libvirt-rust Fix Stream::send

Linus Färnstrand faern at faern.net
Thu Sep 19 05:48:49 UTC 2019


* Handle the -2 error case
* Allow sending arbitrary byte array, not just UTF-8 strings
* Fix FFI declaration, takes size_t, not c_uint
* Return usize to be more idiomatic (type used for slice indexing)

Signed-off-by: Linus Färnstrand <faern at faern.net>
---
 src/stream.rs | 21 ++++++++++-----------
 1 file changed, 10 insertions(+), 11 deletions(-)

diff --git a/src/stream.rs b/src/stream.rs
index af6c8ec..2ca5d0b 100644
--- a/src/stream.rs
+++ b/src/stream.rs
@@ -34,7 +34,7 @@ pub mod sys {
 extern "C" {
     fn virStreamSend(c: sys::virStreamPtr,
                      data: *const libc::c_char,
-                     nbytes: libc::c_uint)
+                     nbytes: libc::size_t)
                      -> libc::c_int;
     fn virStreamRecv(c: sys::virStreamPtr,
                      data: *mut libc::c_char,
@@ -105,16 +105,15 @@ impl Stream {
         }
     }

-    pub fn send(&self, data: &str) -> Result<u32, Error> {
-        unsafe {
-            let ret = virStreamSend(self.as_ptr(),
-                                    string_to_c_chars!(data),
-                                    data.len() as libc::c_uint);
-            if ret == -1 {
-                return Err(Error::new());
-            }
-            return Ok(ret as u32);
-        }
+    pub fn send(&self, data: &[u8]) -> Result<usize, Error> {
+        let ret = unsafe {
+            virStreamSend(
+                self.as_ptr(),
+                data.as_ptr() as *mut libc::c_char,
+                data.len()
+            )
+        };
+        usize::try_from(ret).map_err(|_| Error::new())
     }

     pub fn recv(&self, buf: &mut [u8]) -> Result<usize, Error> {
--
2.21.0





More information about the libvir-list mailing list