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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



* 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@xxxxxxxxx>
---
 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


--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux