1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
#![doc = crate::doc_macro::make_svgbobdoc!(
//! A variable-length array with inline storage.
//!
//! This consists of an integer `length` field, followed immediately (subject to alignment) by the
//! array payload of the specified length. For example, the `VBox<Array<u8>>` representation of `[10u8, 11, 12]` is:
//!
//! ```svgbob
//! "VBox"
//! +----------+
//! | ptr |
//! +----------+
//! |
//! | Array
//! | +-----------+----+----+----+
//! '-> | 3: usize | 10 | 11 | 12 |
//! +-----------+----+----+----+
//! ```
//!
//! Once allocated, the array size may not be modified.
//!
//! # Examples
//!
//! ```
//! use varlen::prelude::*;
//! use varlen::Layout;
//! let a = VBox::<Array<u8>>::new(Array::copy(&[1u8, 2, 3]));
//! assert_eq!(&a[..], &[1, 2, 3]);
//! // Layout is as specified above:
//! assert_eq!(a.calculate_layout().size(), std::mem::size_of::<usize>() + 3)
//! ```
//!
//! # Module contents
//!
//! The main type is [`Array<T>`], valid length types for the array are [`ArrayLen`], its memory layout is
//! calculated and stored in [`ArrayLayout`], and its general initializer is [`SizedInit`].
)]
use crate::array_init::{ArrayInitializer, CloneFrom};
use crate::marker::ArrayMarker;
use crate::{impl_initializer_as_newtype, Initializer, Layout, VClone, VCopy, VarLen};
use core::pin::Pin;
use core::ptr::NonNull;
#[doc = crate::doc_macro::make_svgbobdoc!(
/// An variable-length array with inline storage.
///
/// This consists of an integer length field, followed immediately (subject to alignment) by the
/// array payload of the specified length. For example, the [`crate::VBox<Array<u8>>`] representation of `[10u8, 11, 12]` is:
///
/// ```svgbob
/// "VBox"
/// +----------+
/// | ptr |
/// +----------+
/// |
/// | Array
/// | +-----------+----+----+----+
/// '-> | 3: usize | 10 | 11 | 12 |
/// +-----------+----+----+----+
/// ```
///
/// # Examples
///
/// The `Array` type cannot be stored on the stack, because its size is not known at compile time.
/// Instead, store it in a type such as [`crate::VBox`] or [`crate::Seq`]:
///
/// ```
/// use varlen::prelude::*;
/// let mut a: VBox<Array<u16>> = VBox::new(Array::copy(&[1, 2, 3]));
/// assert_eq!(&a[..], &[1, 2, 3]);
/// a.as_mut().mut_slice()[2] = 5;
/// assert_eq!(&a[..], &[1, 2, 5]);
/// ```
///
/// # Smaller `length` field
///
/// The `Len` type parameter controls what integer type is used to store the array length. This
/// defaults to `usize`, which is long enough to express any array that fits in memory. If this
/// integer type is needlessly large, you may specify a smaller integer type to store the length,
/// at the expense of not supporting longer arrays:
///
/// ```
/// use varlen::prelude::*;
/// // Construction succeeds for short arrays:
/// let arr: VBox<Array<u16, u8>> = VBox::new(Array::try_copy(&[1, 2, 3]).unwrap());
/// assert_eq!(&arr[..], &[1, 2, 3]);
/// // Construction fails for arrays that are too long:
/// assert!(Array::<u16, u8>::try_copy(&[1u16; 257]).is_none());
/// ```
///
/// # Layout
///
/// The type `Array` uses just enough storage for the length, the payload, plus padding alignment:
///
/// ```
/// use varlen::prelude::*;
/// use varlen::Layout;
/// let arr: VBox<Array<u8, u8>> = VBox::new(Array::try_copy(&[1, 2, 3]).unwrap());
/// assert_eq!(1 + 3, arr.calculate_layout().size());
/// ```
)]
pub struct Array<T, Len: ArrayLen = usize> {
len: Len,
_array: ArrayMarker<T>,
}
/// The memory layout of an [`Array`].
///
/// # Examples
///
/// ```
/// use varlen::prelude::*;
/// use varlen::Layout;
/// let arr: VBox<Array<u8, u8>> = VBox::new(Array::try_copy(&[1, 2, 3]).unwrap());
/// assert_eq!(1 + 3, arr.calculate_layout().size());
/// ```
#[derive(PartialEq, Eq)]
pub struct ArrayLayout {
array_offset: usize,
array_len: usize,
size: usize,
}
impl Layout for ArrayLayout {
#[inline(always)]
fn size(&self) -> usize {
self.size
}
}
unsafe impl<T, Len: ArrayLen> VarLen for Array<T, Len> {
type Layout = ArrayLayout;
#[inline(always)]
fn calculate_layout(&self) -> ArrayLayout {
let offset = core::mem::size_of::<Self>();
let (array_offset, array_len, size) =
crate::macro_support::cat_array_field_fast::<T>(self.len.as_usize(), offset);
ArrayLayout {
array_offset,
array_len,
size,
}
}
const ALIGN: usize = crate::macro_support::array_max(&[
core::mem::align_of::<Self>(),
core::mem::align_of::<T>(),
]);
const NEEDS_VDROP: bool = core::mem::needs_drop::<T>();
#[inline(always)]
unsafe fn vdrop(self: core::pin::Pin<&mut Self>, layout: Self::Layout) {
crate::macro_support::drop_array::<T>(
self.get_unchecked_mut() as *mut _ as *mut u8,
layout.array_offset,
layout.array_len,
);
}
}
impl<T, Len: ArrayLen> core::ops::Deref for Array<T, Len> {
type Target = [T];
fn deref(&self) -> &[T] {
let layout = self.calculate_layout();
unsafe { crate::macro_support::ref_array(self, layout.array_offset, layout.array_len) }
}
}
#[allow(rustdoc::missing_doc_code_examples)]
impl<T> Array<T> {
/// Initializes an [`Array`] by copying from an existing slice.
///
/// # Examples
///
/// ```
/// use varlen::prelude::*;
/// let a = VBox::<Array<u8>>::new(Array::copy(&[1, 2, 3]));
/// assert_eq!(&a[..], &[1, 2, 3]);
/// ```
pub fn copy(src: &[T]) -> SizedInit<crate::array_init::CopyFrom<T>>
where
T: Copy,
{
Array::try_copy(src).unwrap()
}
/// Initializes an [`Array`] by cloning from an existing slice.
///
/// # Examples
///
/// ```
/// use varlen::prelude::*;
/// let a: VBox<Array<Box<u16>>> = VBox::new(Array::clone_from_slice(&[Box::new(1), Box::new(2)]));
/// assert_eq!(&a[..], &[Box::new(1), Box::new(2)]);
/// ```
pub fn clone_from_slice(src: &[T]) -> SizedInit<crate::array_init::CloneFrom<T>>
where
T: Clone,
{
Array::try_clone_from_slice(src).unwrap()
}
}
#[allow(rustdoc::missing_doc_code_examples)]
impl<T, Len: ArrayLen> Array<T, Len> {
/// Gets the length of the array at the integer type `Len`.
///
/// # Examples
///
/// ```
/// use varlen::prelude::*;
/// let arr: VBox<Array<u8, u16>> = VBox::new(Array::try_copy(&[1, 2, 3]).unwrap());
/// assert_eq!(3u16, arr.len_short()); // Gets the length as a u16
/// assert_eq!(3usize, arr.len()); // Gets the length as a usize
#[inline]
pub fn len_short(&self) -> Len {
self.len
}
/// Gives mutable access to the payload.
///
/// # Examples
///
/// ```
/// use varlen::prelude::*;
/// let mut a = VBox::<Array<u16>>::new(Array::copy(&[1, 2, 3]));
/// a.as_mut().mut_slice()[2] = 5;
/// assert_eq!(&a[..], &[1, 2, 5]);
/// ```
#[inline]
pub fn mut_slice(self: Pin<&mut Self>) -> &mut [T] {
let layout = self.calculate_layout();
unsafe {
crate::macro_support::mut_array(
self.get_unchecked_mut() as *mut _,
layout.array_offset,
layout.array_len,
)
}
}
/// Initializes an [`Array<T>`] by copying from an existing slice. Returns [`None`] if the slice's length is longer
/// than can fit in this array's `Len` type.
///
/// # Examples
///
/// ```
/// use varlen::prelude::*;
/// // Length fits in u8:
/// let a: VBox<Array<u8, u8>> = VBox::new(Array::try_copy(&[1, 2, 3]).unwrap());
/// assert_eq!(&a[..], &[1, 2, 3]);
/// // Length doesn't fit in u8:
/// assert!(Array::<u8, u8>::try_copy(&[1; 257]).is_none()); // Length is too large for u8.
/// ```
///
/// # See also
///
/// When `Len=usize`, you may prefer [`Array::copy`] which is guaranteed not to fail.
#[inline]
pub fn try_copy(src: &[T]) -> Option<SizedInit<crate::array_init::CopyFrom<T>, Len>>
where
T: Copy,
{
let len = Len::from_usize(src.len())?;
Some(SizedInit(len, crate::array_init::CopyFrom(src)))
}
/// Initializes an [`Array<T>`] by cloning from an existing slice. Returns [`None`] if the slice's length is longer
/// than can fit in this array's `Len` type.
///
/// # Examples
///
/// ```
/// use varlen::prelude::*;
/// // Length fits in u8:
/// let a: VBox<Array<Box<u8>, u8>> = VBox::new(Array::try_clone_from_slice(&[Box::new(1), Box::new(2)]).unwrap());
/// assert_eq!(&a[..], &[Box::new(1), Box::new(2)]);
/// // Length doesn't fit in u8:
/// assert!(Array::<u8, u8>::try_clone_from_slice(&[1; 257]).is_none());
/// ```
///
/// # See also
///
/// When `Len=usize`, you may prefer [`Array::copy`] which is guaranteed not to fail.
pub fn try_clone_from_slice(
src: &[T],
) -> Option<SizedInit<crate::array_init::CloneFrom<T>, Len>>
where
T: Clone,
{
let len = Len::from_usize(src.len())?;
Some(SizedInit(len, crate::array_init::CloneFrom(src)))
}
}
/// Initializer type for cloning an array.
///
/// # Examples
///
/// ```
/// use varlen::prelude::*;
/// let arr = VBox::new(Array::copy(&[1u8, 2, 3]));
/// let seq: Seq<_> = seq![arr.vclone(), arr.vclone(), arr.vclone()]; // Clones the array
/// for a in seq.iter() {
/// assert_eq!(&a[..], &[1, 2, 3]);
/// }
/// ```
pub struct ArrayCloner<'a, T, Len: ArrayLen>(SizedInit<CloneFrom<'a, T>, Len>);
impl_initializer_as_newtype! {
impl<('a), (T: Clone), (Len: ArrayLen)> Initializer<Array<T, Len>> for ArrayCloner<'a, T, Len> { _ }
}
impl<'a, T: 'a + Clone, Len: ArrayLen> VClone<'a> for Array<T, Len> {
type Cloner = ArrayCloner<'a, T, Len>;
fn vclone(&'a self) -> Self::Cloner {
ArrayCloner(SizedInit(self.len_short(), CloneFrom(&self[..])))
}
}
/// Arrays can be copied (fast memcpy) if their elements can.
///
/// # Examples
///
/// ```
/// use varlen::prelude::*;
/// let arr = VBox::new(Array::copy(&[1u8, 2, 3]));
/// let seq: Seq<Array<u8>> = seq![arr.vcopy(), arr.vcopy(), arr.vcopy()]; // Copies the array
/// for a in seq.iter() {
/// assert_eq!(&a[..], &[1, 2, 3]);
/// }
/// ```
// Safety: only fields are T and Len, which both implement Copy.
unsafe impl<'a, T: 'a + Copy, Len: ArrayLen> VCopy<'a> for Array<T, Len> {}
/// Initializer for an [`Array<T>`] given a specified length and array initializer.
///
/// # Examples
///
/// ```
/// use varlen::prelude::*;
/// use varlen::array::SizedInit;
/// use varlen::array_init::FillSequentially;
/// let a: VBox<Array<u8>> = VBox::new(SizedInit(4usize, FillSequentially(|i| (i as u8) * 2)));
/// assert_eq!(&a[..], [0, 2, 4, 6]);
/// ```
///
/// # See also
///
/// Module [`crate::array_init`] has initializers that can be passed to `SizedInit`.
pub struct SizedInit<Init, Len: ArrayLen = usize>(pub Len, pub Init);
unsafe impl<T, Len: ArrayLen, Init: ArrayInitializer<T>> Initializer<Array<T, Len>>
for SizedInit<Init, Len>
{
#[inline(always)]
fn calculate_layout_cautious(&self) -> Option<ArrayLayout> {
let offset = core::mem::size_of::<Array<T, Len>>();
let (array_offset, array_len, size) =
crate::macro_support::cat_array_field_cautious::<T>(self.0.as_usize(), offset)?;
Some(ArrayLayout {
array_offset,
array_len,
size,
})
}
unsafe fn initialize(self, dst: NonNull<Array<T, Len>>, layout: ArrayLayout) {
let header = Array {
len: self.0,
_array: crate::macro_support::init_array(
self.1,
dst.cast::<u8>(),
layout.array_offset,
layout.array_len,
),
};
core::ptr::write(dst.as_ptr(), header);
}
}
/// Integer types that are valid lengths for [`Array<T>`].
///
/// Valid options are: `u8`, `u16`, `u32`, `u64`, `usize`, and nothing else.
///
/// # Examples:
///
/// ```
/// use varlen::prelude::*;
/// assert_eq!(Some(4u8), u8::from_usize(4usize));
/// ```
pub trait ArrayLen: 'static + Copy + private::Sealed {
/// Converts to a [`usize`] via an `as` (truncating) conversion.
///
/// # Examples
///
/// ```
/// use varlen::prelude::*;
/// assert_eq!(4usize, 4u8.as_usize());
/// assert_eq!(u64::MAX as usize, u64::MAX.as_usize());
/// ```
fn as_usize(self) -> usize;
/// Converts from a [`usize`], returning `None` if it doesn't fit in the destination.
///
/// # Examples
///
/// ```
/// use varlen::prelude::*;
/// assert_eq!(Some(4u8), ArrayLen::from_usize(4));
/// assert_eq!(None, u8::from_usize(257));
/// ```
fn from_usize(x: usize) -> Option<Self>;
}
impl ArrayLen for usize {
#[inline(always)]
fn as_usize(self) -> usize {
self
}
#[inline(always)]
fn from_usize(x: usize) -> Option<Self> {
Some(x)
}
}
impl private::Sealed for usize {}
macro_rules! impl_arraylen {
($t:ty) => {
impl ArrayLen for $t {
#[inline(always)]
fn as_usize(self) -> usize {
self as usize
}
#[inline(always)]
fn from_usize(x: usize) -> Option<Self> {
Self::try_from(x).ok()
}
}
impl private::Sealed for $t {}
};
}
impl_arraylen!(u8);
impl_arraylen!(u16);
impl_arraylen!(u32);
impl_arraylen!(u64);
mod private {
pub trait Sealed {}
}