add SliceExt
This commit is contained in:
parent
bd1db0be3a
commit
84c51f5b16
2 changed files with 45 additions and 0 deletions
|
|
@ -1,3 +1,5 @@
|
||||||
mod cchar_ptr;
|
mod cchar_ptr;
|
||||||
|
mod slice;
|
||||||
|
|
||||||
pub(crate) use cchar_ptr::{AsCPtr, CCharPtrExt};
|
pub(crate) use cchar_ptr::{AsCPtr, CCharPtrExt};
|
||||||
|
pub(crate) use slice::SliceExt;
|
||||||
|
|
|
||||||
43
nixide/src/stdext/slice.rs
Normal file
43
nixide/src/stdext/slice.rs
Normal file
|
|
@ -0,0 +1,43 @@
|
||||||
|
use std::iter;
|
||||||
|
use std::ptr::null_mut;
|
||||||
|
|
||||||
|
use crate::util::wrappers::AsInnerPtr;
|
||||||
|
|
||||||
|
pub trait SliceExt<T, S> {
|
||||||
|
#[allow(unused)]
|
||||||
|
fn as_c_array(&self) -> *mut *mut S;
|
||||||
|
|
||||||
|
#[allow(unused)]
|
||||||
|
fn into_c_array(self) -> *mut *mut S;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T, S> SliceExt<T, S> for &[&T]
|
||||||
|
where
|
||||||
|
T: AsInnerPtr<S>,
|
||||||
|
{
|
||||||
|
fn as_c_array(&self) -> *mut *mut S {
|
||||||
|
let mut ptrs: Vec<*mut S> = self
|
||||||
|
.into_iter()
|
||||||
|
.map(|x| unsafe { x.as_ptr() })
|
||||||
|
.chain(iter::once(null_mut()))
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let ptr = ptrs.as_mut_ptr();
|
||||||
|
std::mem::forget(ptrs);
|
||||||
|
|
||||||
|
ptr
|
||||||
|
}
|
||||||
|
|
||||||
|
fn into_c_array(self) -> *mut *mut S {
|
||||||
|
let mut ptrs: Vec<*mut S> = self
|
||||||
|
.into_iter()
|
||||||
|
.map(|x| unsafe { x.as_ptr() })
|
||||||
|
.chain(iter::once(null_mut()))
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let ptr = ptrs.as_mut_ptr();
|
||||||
|
std::mem::forget(ptrs);
|
||||||
|
|
||||||
|
ptr
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue