Skip to content

Commit 8be550c

Browse files
committed
Add Array::to_string method
1 parent 8162a90 commit 8be550c

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

src/core/array.rs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use super::defines::{AfError, Backend, DType};
22
use super::dim4::Dim4;
33
use super::error::HANDLE_ERROR;
4-
use super::util::{af_array, dim_t, void_ptr, HasAfEnum};
4+
use super::util::{af_array, dim_t, free_host, void_ptr, HasAfEnum};
55

66
use libc::{c_char, c_int, c_longlong, c_uint, c_void};
7-
use std::ffi::CString;
7+
use std::ffi::{CStr, CString};
88
use std::fmt;
99
use std::marker::PhantomData;
1010

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

141141
fn af_get_allocated_bytes(result: *mut usize, arr: af_array) -> c_int;
142+
143+
fn af_array_to_string(
144+
ostr: *mut *mut c_char,
145+
exp: *const c_char,
146+
arr: af_array,
147+
precision: c_int,
148+
transpose: bool,
149+
) -> c_int;
142150
}
143151

144152
/// A multidimensional data container
@@ -669,6 +677,26 @@ where
669677
temp
670678
}
671679
}
680+
681+
/// Fetch Array as String
682+
pub fn to_string(&self) -> String {
683+
let result: String;
684+
unsafe {
685+
let cname = CString::new("test").unwrap();
686+
let mut tmp: *mut c_char = ::std::ptr::null_mut();
687+
let err_val = af_array_to_string(
688+
&mut tmp,
689+
cname.to_bytes_with_nul().as_ptr() as *const c_char,
690+
self.get(),
691+
4,
692+
true,
693+
);
694+
HANDLE_ERROR(AfError::from(err_val));
695+
result = CStr::from_ptr(tmp).to_string_lossy().into_owned();
696+
free_host(tmp);
697+
}
698+
result
699+
}
672700
}
673701

674702
/// Used for creating Array object from native

0 commit comments

Comments
 (0)