From 086d07b8599fa4f226154187ec9ab47af6ee608f Mon Sep 17 00:00:00 2001 From: Eivind Alexander Bergem Date: Wed, 23 Aug 2023 08:45:54 +0200 Subject: [PATCH] Added `Buffer::channel_slice_mut()` - Returns underlying channel buffer as mutable slice - Enables writing to buffer --- src/buffer.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/buffer.rs b/src/buffer.rs index 8719da2..5b990f9 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -349,6 +349,23 @@ impl Buffer { } } } + + /// Gets a mutable slice for the data from a channel. + pub fn channel_slice_mut(&mut self, chan: &Channel) -> Option<&mut [T]> { + unsafe { + let begin = ffi::iio_buffer_first(self.buf, chan.chan) as *mut T; + + if begin.is_null() { + None + } else { + let end = ffi::iio_buffer_end(self.buf) as *mut T; + let ptr = begin; + let size = end.offset_from(begin).abs() as usize; + + Some(std::slice::from_raw_parts_mut(ptr, size)) + } + } + } } /// Destroy the underlying buffer when the object scope ends.