MCPcopy Create free account
hub / github.com/dgrunwald/rust-cpython / as_slice

Method as_slice

src/buffer.rs:320–335  ·  view source on GitHub ↗

Gets the buffer memory as a slice. This function succeeds if: the buffer format is compatible with `T` alignment and size of buffer elements is matching the expectations for type `T` the buffer is C-style contiguous The returned slice uses type `Cell ` because it's theoretically possible for any call into the Python runtime to modify the values in the slice.

(&'a self, _py: Python<'a>)

Source from the content-addressed store, hash-verified

318 /// The returned slice uses type `Cell<T>` because it's theoretically possible for any call into the Python runtime
319 /// to modify the values in the slice.
320 pub fn as_slice<'a, T: Element>(&'a self, _py: Python<'a>) -> Option<&'a [ReadOnlyCell<T>]> {
321 if mem::size_of::<T>() == self.item_size()
322 && (self.0.buf as usize) % mem::align_of::<T>() == 0
323 && self.is_c_contiguous()
324 && T::is_compatible_format(self.format())
325 {
326 unsafe {
327 Some(slice::from_raw_parts(
328 self.0.buf as *mut ReadOnlyCell<T>,
329 self.item_count(),
330 ))
331 }
332 } else {
333 None
334 }
335 }
336
337 /// Gets the buffer memory as a slice.
338 ///

Callers

nothing calls this directly

Calls 4

item_sizeMethod · 0.80
is_c_contiguousMethod · 0.80
formatMethod · 0.80
item_countMethod · 0.80

Tested by

no test coverage detected