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

Method to_vec_impl

src/buffer.rs:496–519  ·  view source on GitHub ↗
(&self, py: Python, fort: u8)

Source from the content-addressed store, hash-verified

494 }
495
496 fn to_vec_impl<T: Element + Copy>(&self, py: Python, fort: u8) -> PyResult<Vec<T>> {
497 if !T::is_compatible_format(self.format()) || mem::size_of::<T>() != self.item_size() {
498 incompatible_format_error(py)?;
499 unreachable!();
500 }
501 let item_count = self.item_count();
502 let mut vec: Vec<T> = Vec::with_capacity(item_count);
503 unsafe {
504 // Copy the buffer into the uninitialized space in the vector.
505 // Due to T:Copy, we don't need to be concerned with Drop impls.
506 err::error_on_minusone(
507 py,
508 ffi::PyBuffer_ToContiguous(
509 vec.as_mut_ptr() as *mut libc::c_void,
510 &*self.0 as *const ffi::Py_buffer as *mut ffi::Py_buffer,
511 self.0.len,
512 fort as libc::c_char,
513 ),
514 )?;
515 // set vector length to mark the now-initialized space as usable
516 vec.set_len(item_count);
517 }
518 Ok(vec)
519 }
520
521 /// Copies the specified slice into the buffer.
522 /// If the buffer is multi-dimensional, the elements in the slice are expected to be in C-style order.

Callers 2

to_vecMethod · 0.80
to_fortran_vecMethod · 0.80

Calls 5

error_on_minusoneFunction · 0.85
formatMethod · 0.80
item_sizeMethod · 0.80
item_countMethod · 0.80

Tested by

no test coverage detected