MCPcopy Create free account
hub / github.com/alisomay/libpd-rs / read_float_array_from

Function read_float_array_from

src/functions/array.rs:117–145  ·  view source on GitHub ↗

Reads a named array from pd to a mutable slice of `f32`. Reads values as much as `source_read_amount` from the array which is specified with the `source_name` argument starting from `source_read_offset` and writes them to `destination`. # Example ```no_run use libpd_rs::functions::array::read_float_array_from; let mut destination = [0.0_f32; 64]; read_float_array_from("my_array", 32, 32, &mut d

(
    source_name: T,
    source_read_offset: i32,
    source_read_amount: i32,
    destination: &mut [f32],
)

Source from the content-addressed store, hash-verified

115/// - [`FailedToFindArray`](crate::error::ArrayError::FailedToFindArray)
116/// - [`StringConversion`](crate::error::ArrayError::StringConversion)
117pub fn read_float_array_from<T: AsRef<str>>(
118 source_name: T,
119 source_read_offset: i32,
120 source_read_amount: i32,
121 destination: &mut [f32],
122) -> Result<(), ArrayError> {
123 unsafe {
124 let name = CString::new(source_name.as_ref()).map_err(StringConversionError::from)?;
125 // Returns 0 on success or a negative error code if the array is non-existent
126 // or offset + n exceeds range of array
127
128 if source_read_offset + source_read_amount
129 > array_size(source_name.as_ref()).map_err(|_| ArrayError::FailedToFindArray)?
130 || source_read_amount < 0
131 {
132 return Err(ArrayError::OutOfBounds);
133 }
134
135 match libpd_sys::libpd_read_array(
136 destination.as_mut_ptr(),
137 name.as_ptr(),
138 source_read_offset,
139 source_read_amount,
140 ) {
141 0 => Ok(()),
142 _ => Err(ArrayError::FailedToFindArray),
143 }
144 }
145}
146
147/// Writes a slice of `f32` to a pd named array.
148///

Callers 1

Calls 3

array_sizeFunction · 0.85
as_mut_ptrMethod · 0.80
as_ptrMethod · 0.80

Tested by 1