@@ -1099,23 +1099,33 @@ impl<T: Element> PyArray<T, Ix1> {
10991099 }
11001100 }
11011101
1102+ /// Deprecated form of [`PyArray<T, Ix1>::from_vec_bound`]
1103+ #[ deprecated(
1104+ since = "0.21.0" ,
1105+ note = "will be replaced by `PyArray::from_vec_bound` in the future"
1106+ ) ]
1107+ #[ inline( always) ]
1108+ pub fn from_vec < ' py > ( py : Python < ' py > , vec : Vec < T > ) -> & ' py Self {
1109+ Self :: from_vec_bound ( py, vec) . into_gil_ref ( )
1110+ }
1111+
11021112 /// Construct a one-dimensional array from a [`Vec<T>`][Vec].
11031113 ///
11041114 /// # Example
11051115 ///
11061116 /// ```
1107- /// use numpy::PyArray;
1117+ /// use numpy::{ PyArray, PyArrayMethods} ;
11081118 /// use pyo3::Python;
11091119 ///
11101120 /// Python::with_gil(|py| {
11111121 /// let vec = vec![1, 2, 3, 4, 5];
1112- /// let pyarray = PyArray::from_vec (py, vec);
1122+ /// let pyarray = PyArray::from_vec_bound (py, vec);
11131123 /// assert_eq!(pyarray.readonly().as_slice().unwrap(), &[1, 2, 3, 4, 5]);
11141124 /// });
11151125 /// ```
11161126 #[ inline( always) ]
1117- pub fn from_vec < ' py > ( py : Python < ' py > , vec : Vec < T > ) -> & ' py Self {
1118- vec. into_pyarray_bound ( py) . into_gil_ref ( )
1127+ pub fn from_vec_bound < ' py > ( py : Python < ' py > , vec : Vec < T > ) -> Bound < ' py , Self > {
1128+ vec. into_pyarray_bound ( py)
11191129 }
11201130
11211131 /// Deprecated form of [`PyArray<T, Ix1>::from_iter_bound`]
@@ -1156,6 +1166,15 @@ impl<T: Element> PyArray<T, Ix1> {
11561166}
11571167
11581168impl < T : Element > PyArray < T , Ix2 > {
1169+ /// Deprecated form of [`PyArray<T, Ix2>::from_vec2_bound`]
1170+ #[ deprecated(
1171+ since = "0.21.0" ,
1172+ note = "will be replaced by `PyArray::from_vec2_bound` in the future"
1173+ ) ]
1174+ pub fn from_vec2 < ' py > ( py : Python < ' py > , v : & [ Vec < T > ] ) -> Result < & ' py Self , FromVecError > {
1175+ Self :: from_vec2_bound ( py, v) . map ( Bound :: into_gil_ref)
1176+ }
1177+
11591178 /// Construct a two-dimension array from a [`Vec<Vec<T>>`][Vec].
11601179 ///
11611180 /// This function checks all dimensions of the inner vectors and returns
@@ -1164,20 +1183,23 @@ impl<T: Element> PyArray<T, Ix2> {
11641183 /// # Example
11651184 ///
11661185 /// ```
1167- /// use numpy::PyArray;
1186+ /// use numpy::{ PyArray, PyArrayMethods} ;
11681187 /// use pyo3::Python;
11691188 /// use ndarray::array;
11701189 ///
11711190 /// Python::with_gil(|py| {
11721191 /// let vec2 = vec![vec![11, 12], vec![21, 22]];
1173- /// let pyarray = PyArray::from_vec2 (py, &vec2).unwrap();
1192+ /// let pyarray = PyArray::from_vec2_bound (py, &vec2).unwrap();
11741193 /// assert_eq!(pyarray.readonly().as_array(), array![[11, 12], [21, 22]]);
11751194 ///
11761195 /// let ragged_vec2 = vec![vec![11, 12], vec![21]];
1177- /// assert!(PyArray::from_vec2 (py, &ragged_vec2).is_err());
1196+ /// assert!(PyArray::from_vec2_bound (py, &ragged_vec2).is_err());
11781197 /// });
11791198 /// ```
1180- pub fn from_vec2 < ' py > ( py : Python < ' py > , v : & [ Vec < T > ] ) -> Result < & ' py Self , FromVecError > {
1199+ pub fn from_vec2_bound < ' py > (
1200+ py : Python < ' py > ,
1201+ v : & [ Vec < T > ] ,
1202+ ) -> Result < Bound < ' py , Self > , FromVecError > {
11811203 let len2 = v. first ( ) . map_or ( 0 , |v| v. len ( ) ) ;
11821204 let dims = [ v. len ( ) , len2] ;
11831205 // SAFETY: The result of `Self::new` is always safe to drop.
@@ -1191,12 +1213,21 @@ impl<T: Element> PyArray<T, Ix2> {
11911213 }
11921214 clone_elements ( v, & mut data_ptr) ;
11931215 }
1194- Ok ( array. into_gil_ref ( ) )
1216+ Ok ( array)
11951217 }
11961218 }
11971219}
11981220
11991221impl < T : Element > PyArray < T , Ix3 > {
1222+ /// Deprecated form of [`PyArray<T, Ix3>::from_vec3_bound`]
1223+ #[ deprecated(
1224+ since = "0.21.0" ,
1225+ note = "will be replaced by `PyArray::from_vec3_bound` in the future"
1226+ ) ]
1227+ pub fn from_vec3 < ' py > ( py : Python < ' py > , v : & [ Vec < Vec < T > > ] ) -> Result < & ' py Self , FromVecError > {
1228+ Self :: from_vec3_bound ( py, v) . map ( Bound :: into_gil_ref)
1229+ }
1230+
12001231 /// Construct a three-dimensional array from a [`Vec<Vec<Vec<T>>>`][Vec].
12011232 ///
12021233 /// This function checks all dimensions of the inner vectors and returns
@@ -1205,7 +1236,7 @@ impl<T: Element> PyArray<T, Ix3> {
12051236 /// # Example
12061237 ///
12071238 /// ```
1208- /// use numpy::PyArray;
1239+ /// use numpy::{ PyArray, PyArrayMethods} ;
12091240 /// use pyo3::Python;
12101241 /// use ndarray::array;
12111242 ///
@@ -1214,7 +1245,7 @@ impl<T: Element> PyArray<T, Ix3> {
12141245 /// vec![vec![111, 112], vec![121, 122]],
12151246 /// vec![vec![211, 212], vec![221, 222]],
12161247 /// ];
1217- /// let pyarray = PyArray::from_vec3 (py, &vec3).unwrap();
1248+ /// let pyarray = PyArray::from_vec3_bound (py, &vec3).unwrap();
12181249 /// assert_eq!(
12191250 /// pyarray.readonly().as_array(),
12201251 /// array![[[111, 112], [121, 122]], [[211, 212], [221, 222]]]
@@ -1224,10 +1255,13 @@ impl<T: Element> PyArray<T, Ix3> {
12241255 /// vec![vec![111, 112], vec![121, 122]],
12251256 /// vec![vec![211], vec![221, 222]],
12261257 /// ];
1227- /// assert!(PyArray::from_vec3 (py, &ragged_vec3).is_err());
1258+ /// assert!(PyArray::from_vec3_bound (py, &ragged_vec3).is_err());
12281259 /// });
12291260 /// ```
1230- pub fn from_vec3 < ' py > ( py : Python < ' py > , v : & [ Vec < Vec < T > > ] ) -> Result < & ' py Self , FromVecError > {
1261+ pub fn from_vec3_bound < ' py > (
1262+ py : Python < ' py > ,
1263+ v : & [ Vec < Vec < T > > ] ,
1264+ ) -> Result < Bound < ' py , Self > , FromVecError > {
12311265 let len2 = v. first ( ) . map_or ( 0 , |v| v. len ( ) ) ;
12321266 let len3 = v. first ( ) . map_or ( 0 , |v| v. first ( ) . map_or ( 0 , |v| v. len ( ) ) ) ;
12331267 let dims = [ v. len ( ) , len2, len3] ;
@@ -1248,7 +1282,7 @@ impl<T: Element> PyArray<T, Ix3> {
12481282 clone_elements ( v, & mut data_ptr) ;
12491283 }
12501284 }
1251- Ok ( array. into_gil_ref ( ) )
1285+ Ok ( array)
12521286 }
12531287 }
12541288}
@@ -2319,7 +2353,7 @@ mod tests {
23192353 #[ test]
23202354 fn test_dyn_to_owned_array ( ) {
23212355 Python :: with_gil ( |py| {
2322- let array = PyArray :: from_vec2 ( py, & [ vec ! [ 1 , 2 ] , vec ! [ 3 , 4 ] ] )
2356+ let array = PyArray :: from_vec2_bound ( py, & [ vec ! [ 1 , 2 ] , vec ! [ 3 , 4 ] ] )
23232357 . unwrap ( )
23242358 . to_dyn ( )
23252359 . to_owned_array ( ) ;
0 commit comments