|  | 
|  | 1 | +//! `Storage` trait defining how data is stored in a container. | 
|  | 2 | +
 | 
|  | 3 | +use core::borrow::{Borrow, BorrowMut}; | 
|  | 4 | + | 
|  | 5 | +pub(crate) trait SealedStorage { | 
|  | 6 | +    type Buffer<T>: ?Sized + Borrow<[T]> + BorrowMut<[T]>; | 
|  | 7 | +} | 
|  | 8 | + | 
|  | 9 | +/// Trait defining how data for a container is stored. | 
|  | 10 | +/// | 
|  | 11 | +/// There's two implementations available: | 
|  | 12 | +/// | 
|  | 13 | +/// - [`OwnedStorage`]: stores the data in an array `[T; N]` whose size is known at compile time. | 
|  | 14 | +/// - [`ViewStorage`]: stores the data in an unsized `[T]`. | 
|  | 15 | +/// | 
|  | 16 | +/// This allows containers to be generic over either sized or unsized storage. For example, | 
|  | 17 | +/// the [`vec`](crate::vec) module contains a [`VecInner`](crate::vec::VecInner) struct | 
|  | 18 | +/// that's generic on [`Storage`], and two type aliases for convenience: | 
|  | 19 | +/// | 
|  | 20 | +/// - [`Vec<T, N>`](crate::vec::Vec) = `VecInner<T, OwnedStorage<N>>` | 
|  | 21 | +/// - [`VecView<T>`](crate::vec::VecView) = `VecInner<T, ViewStorage>` | 
|  | 22 | +/// | 
|  | 23 | +/// `Vec` can be unsized into `VecView`, either by unsizing coercions such as `&mut Vec -> &mut VecView` or | 
|  | 24 | +/// `Box<Vec> -> Box<VecView>`, or explicitly with [`.as_view()`](crate::vec::Vec::as_view) or [`.as_mut_view()`](crate::vec::Vec::as_mut_view). | 
|  | 25 | +/// | 
|  | 26 | +/// This trait is sealed, so you cannot implement it for your own types. You can only use | 
|  | 27 | +/// the implementations provided by this crate. | 
|  | 28 | +#[allow(private_bounds)] | 
|  | 29 | +pub trait Storage: SealedStorage {} | 
|  | 30 | + | 
|  | 31 | +/// Implementation of [`Storage`] that stores the data in an array `[T; N]` whose size is known at compile time. | 
|  | 32 | +pub enum OwnedStorage<const N: usize> {} | 
|  | 33 | +impl<const N: usize> Storage for OwnedStorage<N> {} | 
|  | 34 | +impl<const N: usize> SealedStorage for OwnedStorage<N> { | 
|  | 35 | +    type Buffer<T> = [T; N]; | 
|  | 36 | +} | 
|  | 37 | + | 
|  | 38 | +/// Implementation of [`Storage`] that stores the data in an unsized `[T]`. | 
|  | 39 | +pub enum ViewStorage {} | 
|  | 40 | +impl Storage for ViewStorage {} | 
|  | 41 | +impl SealedStorage for ViewStorage { | 
|  | 42 | +    type Buffer<T> = [T]; | 
|  | 43 | +} | 
0 commit comments