/* This Source Code Form is subject to the terms of the Mozilla Public *License,v.2.0.IfacopyoftheMPLwasnotdistributedwiththis
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#![allow(unsafe_code)]
//! A replacement for `Box<[T]>` that cbindgen can understand.
use malloc_size_of::{MallocShallowSizeOf, MallocSizeOf, MallocSizeOfOps}; use serde::de::{Deserialize, Deserializer}; use serde::ser::{Serialize, Serializer}; use std::marker::PhantomData; use std::ops::{Deref, DerefMut}; use std::ptr::NonNull; use std::{fmt, iter, mem, slice, hash::{Hash, Hasher}}; use to_shmem::{SharedMemoryBuilder, ToShmem};
/// A struct that basically replaces a `Box<[T]>`, but which cbindgen can /// understand. /// /// We could rely on the struct layout of `Box<[T]>` per: /// /// https://github.com/rust-lang/unsafe-code-guidelines/blob/master/reference/src/layout/pointers.md /// /// But handling fat pointers with cbindgen both in structs and argument /// positions more generally is a bit tricky. /// /// cbindgen:derive-eq=false /// cbindgen:derive-neq=false #[repr(C)] pubstruct OwnedSlice<T: Sized> {
ptr: NonNull<T>,
len: usize,
_phantom: PhantomData<T>,
}
impl<T: Sized> OwnedSlice<T> { /// Convert the OwnedSlice into a boxed slice. #[inline] pubfn into_box(self) -> Box<[T]> { self.into_vec().into_boxed_slice()
}
/// Convert the OwnedSlice into a Vec. #[inline] pubfn into_vec(self) -> Vec<T> { let ret = unsafe { Vec::from_raw_parts(self.ptr.as_ptr(), self.len, self.len) };
mem::forget(self);
ret
}
/// Convert the regular slice into an owned slice. #[inline] pubfn from_slice(s: &[T]) -> Self where
T: Clone,
{ Self::from(s.to_vec())
}
}
impl<T> IntoIterator for OwnedSlice<T> { type Item = T; type IntoIter = <Vec<T> as IntoIterator>::IntoIter;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.