|
161 | 161 | //! [base]: https://numpy.org/doc/stable/reference/c-api/types-and-structures.html#c.NPY_AO.base
|
162 | 162 | #![deny(missing_docs)]
|
163 | 163 |
|
| 164 | +use std::any::type_name; |
164 | 165 | use std::cell::UnsafeCell;
|
165 | 166 | use std::collections::hash_map::{Entry, HashMap};
|
| 167 | +use std::fmt; |
166 | 168 | use std::mem::size_of;
|
167 | 169 | use std::ops::Deref;
|
168 | 170 |
|
@@ -500,6 +502,22 @@ where
|
500 | 502 | }
|
501 | 503 | }
|
502 | 504 |
|
| 505 | +impl<'py, T, D> fmt::Debug for PyReadonlyArray<'py, T, D> |
| 506 | +where |
| 507 | + T: Element, |
| 508 | + D: Dimension, |
| 509 | +{ |
| 510 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 511 | + let name = format!( |
| 512 | + "PyReadonlyArray<{}, {}>", |
| 513 | + type_name::<T>(), |
| 514 | + type_name::<D>() |
| 515 | + ); |
| 516 | + |
| 517 | + f.debug_struct(&name).finish() |
| 518 | + } |
| 519 | +} |
| 520 | + |
503 | 521 | /// Read-write borrow of an array.
|
504 | 522 | ///
|
505 | 523 | /// An instance of this type ensures that there are no instances of [`PyReadonlyArray`] and no other instances of [`PyReadwriteArray`],
|
@@ -636,6 +654,22 @@ where
|
636 | 654 | }
|
637 | 655 | }
|
638 | 656 |
|
| 657 | +impl<'py, T, D> fmt::Debug for PyReadwriteArray<'py, T, D> |
| 658 | +where |
| 659 | + T: Element, |
| 660 | + D: Dimension, |
| 661 | +{ |
| 662 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 663 | + let name = format!( |
| 664 | + "PyReadwriteArray<{}, {}>", |
| 665 | + type_name::<T>(), |
| 666 | + type_name::<D>() |
| 667 | + ); |
| 668 | + |
| 669 | + f.debug_struct(&name).finish() |
| 670 | + } |
| 671 | +} |
| 672 | + |
639 | 673 | fn base_address<T, D>(array: &PyArray<T, D>) -> usize {
|
640 | 674 | let py = array.py();
|
641 | 675 | let mut array = array.as_array_ptr();
|
@@ -1210,4 +1244,29 @@ mod tests {
|
1210 | 1244 | }
|
1211 | 1245 | });
|
1212 | 1246 | }
|
| 1247 | + |
| 1248 | + #[test] |
| 1249 | + fn test_debug_formatting() { |
| 1250 | + Python::with_gil(|py| { |
| 1251 | + let array = PyArray::<f64, _>::zeros(py, (1, 2, 3), false); |
| 1252 | + |
| 1253 | + { |
| 1254 | + let shared = array.readonly(); |
| 1255 | + |
| 1256 | + assert_eq!( |
| 1257 | + format!("{:?}", shared), |
| 1258 | + "PyReadonlyArray<f64, ndarray::dimension::dim::Dim<[usize; 3]>>" |
| 1259 | + ); |
| 1260 | + } |
| 1261 | + |
| 1262 | + { |
| 1263 | + let exclusive = array.readwrite(); |
| 1264 | + |
| 1265 | + assert_eq!( |
| 1266 | + format!("{:?}", exclusive), |
| 1267 | + "PyReadwriteArray<f64, ndarray::dimension::dim::Dim<[usize; 3]>>" |
| 1268 | + ); |
| 1269 | + } |
| 1270 | + }); |
| 1271 | + } |
1213 | 1272 | }
|
0 commit comments