MCPcopy Index your code
hub / github.com/PyO3/rust-numpy / from_vec2

Method from_vec2

src/array.rs:595–611  ·  view source on GitHub ↗

Construct a two-dimension array from a [`Vec >`][Vec]. This function checks all dimensions of the inner vectors and returns an error if they are not all equal. # Example ``` use numpy::{PyArray, PyArrayMethods}; use pyo3::Python; use ndarray::array; Python::attach(|py| { let vec2 = vec![vec![11, 12], vec![21, 22]]; let pyarray = PyArray::from_vec2(py, &vec2).unwrap(); assert_eq!(pyarray.

(py: Python<'py>, v: &[Vec<T>])

Source from the content-addressed store, hash-verified

593 /// });
594 /// ```
595 pub fn from_vec2<'py>(py: Python<'py>, v: &[Vec<T>]) -> Result<Bound<'py, Self>, FromVecError> {
596 let len2 = v.first().map_or(0, |v| v.len());
597 let dims = [v.len(), len2];
598 // SAFETY: The result of `Self::new` is always safe to drop.
599 unsafe {
600 let array = Self::new(py, dims, false);
601 let mut data_ptr = array.data();
602 for v in v {
603 if v.len() != len2 {
604 cold();
605 return Err(FromVecError::new(v.len(), len2));
606 }
607 clone_elements(py, v, &mut data_ptr);
608 }
609 Ok(array)
610 }
611 }
612}
613
614impl<T: Element> PyArray<T, Ix3> {

Callers

nothing calls this directly

Calls 4

coldFunction · 0.85
clone_elementsFunction · 0.85
lenMethod · 0.80
dataMethod · 0.80

Tested by

no test coverage detected