Skip to content

Commit 26c8a04

Browse files
committed
Implement Debug Trait for Array
1 parent 3ec21a7 commit 26c8a04

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/core/array.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use super::util::{af_array, dim_t, void_ptr, HasAfEnum};
55

66
use libc::{c_char, c_int, c_longlong, c_uint, c_void};
77
use std::ffi::CString;
8+
use std::fmt;
89
use std::marker::PhantomData;
910

1011
// Some unused functions from array.h in C-API of ArrayFire
@@ -853,6 +854,24 @@ pub fn is_eval_manual() -> bool {
853854
}
854855
}
855856

857+
/// Prints data type, shape and data of a given Array in programming friendly context
858+
///
859+
/// Used via println macro or formatter
860+
impl<T> fmt::Debug for Array<T>
861+
where
862+
T: HasAfEnum,
863+
{
864+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
865+
let mut vec = vec![T::default(); self.elements()];
866+
self.host(&mut vec);
867+
f.debug_struct("Array")
868+
.field("dtype", &self.get_type())
869+
.field("shape", &self.dims())
870+
.field("data", &vec)
871+
.finish()
872+
}
873+
}
874+
856875
#[cfg(feature = "afserde")]
857876
mod afserde {
858877
// Reimport required from super scope

0 commit comments

Comments
 (0)