Skip to content

Commit 1fdbc75

Browse files
committed
Add Array::to_string method
1 parent df79646 commit 1fdbc75

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
@@ -672,6 +680,26 @@ where
672680
temp
673681
}
674682
}
683+
684+
/// Fetch Array as String
685+
pub fn to_string(&self) -> String {
686+
let result: String;
687+
unsafe {
688+
let cname = CString::new("test").unwrap();
689+
let mut tmp: *mut c_char = ::std::ptr::null_mut();
690+
let err_val = af_array_to_string(
691+
&mut tmp,
692+
cname.to_bytes_with_nul().as_ptr() as *const c_char,
693+
self.get(),
694+
4,
695+
true,
696+
);
697+
HANDLE_ERROR(AfError::from(err_val));
698+
result = CStr::from_ptr(tmp).to_string_lossy().into_owned();
699+
free_host(tmp);
700+
}
701+
result
702+
}
675703
}
676704

677705
/// Used for creating Array object from native

0 commit comments

Comments
 (0)