#[cfg(feature = "read")] use alloc::boxed::Box; #[cfg(feature = "read")] use alloc::vec::Vec; use core::fmt; use core::mem::MaybeUninit; use core::ops; use core::ptr; use core::slice;
mod sealed { /// # Safety /// Implementer must not modify the content in storage. pubunsafetrait Sealed { type Storage;
/// Marker trait for types that can be used as backing storage when a growable array type is needed. /// /// This trait is sealed and cannot be implemented for types outside this crate. pubtrait ArrayLike: Sealed { /// Type of the elements being stored. type Item;
// Use macro since const generics can't be used due to MSRV.
macro_rules! impl_array {
() => {};
($n:literal $($rest:tt)*) => { // SAFETY: does not modify the content in storage. unsafeimpl<T> Sealed for [T; $n] { type Storage = [MaybeUninit<T>; $n];
fn new_storage() -> Self::Storage { // SAFETY: An uninitialized `[MaybeUninit<_>; _]` is valid. unsafe { MaybeUninit::uninit().assume_init() }
}
}
pubfn clear(&mutself) { let ptr: *mut [A::Item] = &mut **self; // Set length first so the type invariant is upheld even if `drop_in_place` panicks. self.len = 0; // SAFETY: `ptr` contains valid elements only and we "forget" them by setting the length. unsafe { ptr::drop_in_place(ptr) };
}
// SAFETY: storage[index] is filled later. unsafe { let p = storage.as_mut_ptr().add(index);
core::ptr::copy(p as *const _, p.add(1), self.len - index);
}
storage[index] = MaybeUninit::new(element); self.len += 1;
Ok(())
}
pubfn pop(&mutself) -> Option<A::Item> { ifself.len == 0 {
None
} else { self.len -= 1; // SAFETY: this element is valid and we "forget" it by setting the length.
Some(unsafe { A::as_slice(&self.storage)[self.len].as_ptr().read() })
}
}
impl<A: ArrayLike> Clone for ArrayVec<A> where
A::Item: Clone,
{ fn clone(&self) -> Self { letmut new = Self::default(); for value in &**self {
new.try_push(value.clone()).unwrap();
}
new
}
}
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.