From 54daace91590d074d6c9d6d644e1d8dca8166ff2 Mon Sep 17 00:00:00 2001 From: pradeep Date: Mon, 18 Jan 2021 18:55:52 +0530 Subject: [PATCH 1/2] Extend HasAfEnum with Default, Copy and Debug traits --- src/core/arith.rs | 2 +- src/core/array.rs | 4 ++-- src/core/util.rs | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/core/arith.rs b/src/core/arith.rs index 86a4681cb..3c978eef8 100644 --- a/src/core/arith.rs +++ b/src/core/arith.rs @@ -383,7 +383,7 @@ where type OutType = T; fn convert(&self) -> Array { - constant(self.clone(), Dim4::new(&[1, 1, 1, 1])) + constant(*self, Dim4::new(&[1, 1, 1, 1])) } } diff --git a/src/core/array.rs b/src/core/array.rs index 9d4267569..2518b1bb8 100644 --- a/src/core/array.rs +++ b/src/core/array.rs @@ -872,7 +872,7 @@ mod afserde { /// Serialize Implementation of Array impl Serialize for Array where - T: std::default::Default + std::clone::Clone + Serialize + HasAfEnum + std::fmt::Debug, + T: Serialize + HasAfEnum, { fn serialize(&self, serializer: S) -> Result where @@ -892,7 +892,7 @@ mod afserde { /// Deserialize Implementation of Array impl<'de, T> Deserialize<'de> for Array where - T: Deserialize<'de> + HasAfEnum + std::fmt::Debug, + T: Deserialize<'de> + HasAfEnum, { fn deserialize(deserializer: D) -> Result where diff --git a/src/core/util.rs b/src/core/util.rs index 253695f31..b43635820 100644 --- a/src/core/util.rs +++ b/src/core/util.rs @@ -120,7 +120,7 @@ impl From for ColorMap { } mod private { - pub trait Sealed {} + pub trait Sealed: std::fmt::Debug + std::marker::Copy + std::default::Default {} impl Sealed for num::complex::Complex {} impl Sealed for num::complex::Complex {} From 73437f311e179c26e89ebd0eb24ff0b107682e80 Mon Sep 17 00:00:00 2001 From: pradeep Date: Mon, 18 Jan 2021 19:02:49 +0530 Subject: [PATCH 2/2] Implement Debug Trait for Array --- src/core/array.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/core/array.rs b/src/core/array.rs index 2518b1bb8..6e22a10fc 100644 --- a/src/core/array.rs +++ b/src/core/array.rs @@ -5,6 +5,7 @@ use super::util::{af_array, dim_t, void_ptr, HasAfEnum}; use libc::{c_char, c_int, c_longlong, c_uint, c_void}; use std::ffi::CString; +use std::fmt; use std::marker::PhantomData; // Some unused functions from array.h in C-API of ArrayFire @@ -853,6 +854,24 @@ pub fn is_eval_manual() -> bool { } } +/// Prints data type, shape and data of a given Array in programming friendly context +/// +/// Used via println macro or formatter +impl fmt::Debug for Array +where + T: HasAfEnum, +{ + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let mut vec = vec![T::default(); self.elements()]; + self.host(&mut vec); + f.debug_struct("Array") + .field("dtype", &self.get_type()) + .field("shape", &self.dims()) + .field("data", &vec) + .finish() + } +} + #[cfg(feature = "afserde")] mod afserde { // Reimport required from super scope