tinyvec/
array.rs

1/// A trait for types that are an array.
2///
3/// An "array", for our purposes, has the following properties:
4/// * Owns some number of elements.
5/// * The element type can be generic, but must implement [`Default`].
6/// * The capacity is fixed at compile time, based on the implementing type.
7/// * You can get a shared or mutable slice to the elements.
8///
9/// You are generally **not** expected to need to implement this yourself. It is
10/// already implemented for all array lengths.
11///
12/// **Additional lengths can easily be added upon request.**
13///
14/// ## Safety Reminder
15///
16/// Just a reminder: this trait is 100% safe, which means that `unsafe` code
17/// **must not** rely on an instance of this trait being correct.
18pub trait Array {
19  /// The type of the items in the thing.
20  type Item: Default;
21
22  /// The number of slots in the thing.
23  const CAPACITY: usize;
24
25  /// Gives a shared slice over the whole thing.
26  ///
27  /// A correct implementation will return a slice with a length equal to the
28  /// `CAPACITY` value.
29  fn as_slice(&self) -> &[Self::Item];
30
31  /// Gives a unique slice over the whole thing.
32  ///
33  /// A correct implementation will return a slice with a length equal to the
34  /// `CAPACITY` value.
35  fn as_slice_mut(&mut self) -> &mut [Self::Item];
36
37  /// Create a default-initialized instance of ourself, similar to the
38  /// [`Default`] trait, but implemented for the same range of sizes as
39  /// [`Array`].
40  fn default() -> Self;
41}
42
43mod const_generic_impl;
44
45#[cfg(feature = "generic-array")]
46mod generic_array_impl;