Skip to content

Format debug trait output #275

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 48 additions & 9 deletions src/core/array.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use super::defines::{AfError, Backend, DType};
use super::dim4::Dim4;
use super::error::HANDLE_ERROR;
use super::util::{af_array, dim_t, void_ptr, HasAfEnum};
use super::util::{af_array, dim_t, free_host, void_ptr, HasAfEnum};

use libc::{c_char, c_int, c_longlong, c_uint, c_void};
use std::ffi::CString;
use std::ffi::{CStr, CString};
use std::fmt;
use std::marker::PhantomData;

Expand Down Expand Up @@ -139,6 +139,14 @@ extern "C" {
fn af_get_device_ptr(ptr: *mut void_ptr, arr: af_array) -> c_int;

fn af_get_allocated_bytes(result: *mut usize, arr: af_array) -> c_int;

fn af_array_to_string(
ostr: *mut *mut c_char,
exp: *const c_char,
arr: af_array,
precision: c_int,
transpose: bool,
) -> c_int;
}

/// A multidimensional data container
Expand Down Expand Up @@ -672,6 +680,26 @@ where
temp
}
}

/// Fetch Array as String
pub fn to_string(&self) -> String {
let result: String;
unsafe {
let cname = CString::new("test").unwrap();
let mut tmp: *mut c_char = ::std::ptr::null_mut();
let err_val = af_array_to_string(
&mut tmp,
cname.to_bytes_with_nul().as_ptr() as *const c_char,
self.get(),
4,
true,
);
HANDLE_ERROR(AfError::from(err_val));
result = CStr::from_ptr(tmp).to_string_lossy().into_owned();
free_host(tmp);
}
result
}
}

/// Used for creating Array object from native
Expand Down Expand Up @@ -862,13 +890,24 @@ 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()
if f.alternate() {
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("strides", &self.strides())
.field("offset", &self.offset())
.field("device_id", &self.get_device_id())
.field("data", &vec)
.finish()
} else {
f.debug_struct("Array")
.field("dtype", &self.get_type())
.field("shape", &self.dims())
.field("af_array", unsafe { &self.get() })
.finish()
}
}
}

Expand Down
26 changes: 23 additions & 3 deletions src/core/dim4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::ops::{Index, IndexMut};
use serde::{Deserialize, Serialize};

/// Dim4 is used to store [Array](./struct.Array.html) dimensions
#[derive(Copy, Clone, PartialEq, Debug)]
#[derive(Copy, Clone, PartialEq)]
#[cfg_attr(feature = "afserde", derive(Serialize, Deserialize))]
pub struct Dim4 {
dims: [u64; 4],
Expand Down Expand Up @@ -64,13 +64,33 @@ impl IndexMut<usize> for Dim4 {
/// use arrayfire::Dim4;
///
/// let dims = Dim4::new(&[4, 4, 2, 1]);
/// println!("0th Dimension length is {}", dims[0]); // -> 4
/// println!("Shape is {}", dims); // -> [4, 4, 2, 1]
/// ```
impl fmt::Display for Dim4 {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"[{} {} {} {}]",
"[{}, {}, {}, {}]",
self.dims[0], self.dims[1], self.dims[2], self.dims[3]
)
}
}

/// Debug trait implementation for Dim4 objects
///
/// # Examples
///
/// ```rust
/// use arrayfire::Dim4;
///
/// let dims = Dim4::new(&[4, 4, 2, 1]);
/// println!("Shape is {:?}", dims); // -> {4, 4, 2, 1}
/// ```
impl fmt::Debug for Dim4 {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"[{}, {}, {}, {}]",
self.dims[0], self.dims[1], self.dims[2], self.dims[3]
)
}
Expand Down