Reputation: 2280
I remember there being a function array_each
but that's now deprecated. Was this replaced with another function? If not, how can I use the C pointer to an array as a rust array?
extern {
fn testing() -> *MyList
}
#[repr(C)]
struct MyList;
fn main() {
unsafe {
let list = testing();
// would like to iterate through the list here
}
}
Upvotes: 0
Views: 659
Reputation: 516
Use std::slice::from_raw_parts
:
extern {
fn get_some_list(len: *mut u32) -> *mut u32;
}
fn main() {
use std::slice;
unsafe {
let mut len: u32 = 0;
let ptr: *mut u32 = get_some_list(&mut len);
assert!(!ptr.is_null());
let view: &[u32] = slice::from_raw_parts(ptr, len as usize);
for &v in view.iter() { println!("{}", v); }
}
}
The resulting slice may have an arbitrary lifetime (including 'static
), so be careful about exposing that slice to the safe outside.
(Note: Your original code uses some old syntax, which I've fixed in this answer. For example, you need const
or mut
after *
now.)
Upvotes: 3