-
Notifications
You must be signed in to change notification settings - Fork 193
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #486 from Dirbaio/vecview-dedup
vec: remove code duplication due to VecView.
- Loading branch information
Showing
6 changed files
with
477 additions
and
1,052 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
//! `Storage` trait defining how data is stored in a container. | ||
use core::borrow::{Borrow, BorrowMut}; | ||
|
||
pub(crate) trait SealedStorage { | ||
type Buffer<T>: ?Sized + Borrow<[T]> + BorrowMut<[T]>; | ||
} | ||
|
||
/// Trait defining how data for a container is stored. | ||
/// | ||
/// There's two implementations available: | ||
/// | ||
/// - [`OwnedStorage`]: stores the data in an array `[T; N]` whose size is known at compile time. | ||
/// - [`ViewStorage`]: stores the data in an unsized `[T]`. | ||
/// | ||
/// This allows containers to be generic over either sized or unsized storage. For example, | ||
/// the [`vec`](crate::vec) module contains a [`VecInner`](crate::vec::VecInner) struct | ||
/// that's generic on [`Storage`], and two type aliases for convenience: | ||
/// | ||
/// - [`Vec<T, N>`](crate::vec::Vec) = `VecInner<T, OwnedStorage<N>>` | ||
/// - [`VecView<T>`](crate::vec::VecView) = `VecInner<T, ViewStorage>` | ||
/// | ||
/// `Vec` can be unsized into `VecView`, either by unsizing coercions such as `&mut Vec -> &mut VecView` or | ||
/// `Box<Vec> -> Box<VecView>`, or explicitly with [`.as_view()`](crate::vec::Vec::as_view) or [`.as_mut_view()`](crate::vec::Vec::as_mut_view). | ||
/// | ||
/// This trait is sealed, so you cannot implement it for your own types. You can only use | ||
/// the implementations provided by this crate. | ||
#[allow(private_bounds)] | ||
pub trait Storage: SealedStorage {} | ||
|
||
/// Implementation of [`Storage`] that stores the data in an array `[T; N]` whose size is known at compile time. | ||
pub enum OwnedStorage<const N: usize> {} | ||
impl<const N: usize> Storage for OwnedStorage<N> {} | ||
impl<const N: usize> SealedStorage for OwnedStorage<N> { | ||
type Buffer<T> = [T; N]; | ||
} | ||
|
||
/// Implementation of [`Storage`] that stores the data in an unsized `[T]`. | ||
pub enum ViewStorage {} | ||
impl Storage for ViewStorage {} | ||
impl SealedStorage for ViewStorage { | ||
type Buffer<T> = [T]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.