vec: remove code duplication due to VecView.

This commit is contained in:
Dario Nieuwenhuis 2024-06-27 20:34:43 +02:00
parent 1f25e658ca
commit e9f8bac7ec
3 changed files with 571 additions and 1164 deletions

View File

@ -96,13 +96,6 @@ pub use indexmap::{
pub use indexset::{FnvIndexSet, IndexSet, Iter as IndexSetIter};
pub use linear_map::LinearMap;
pub use string::String;
// Workaround https://github.com/rust-lang/rust/issues/119015. This is required so that the methods on `VecView` and `Vec` are properly documented.
// cfg(doc) prevents `VecInner` being part of the public API.
// doc(hidden) prevents the `pub use vec::VecInner` from being visible in the documentation.
#[cfg(doc)]
#[doc(hidden)]
pub use vec::VecInner as _;
pub use vec::{Vec, VecView};
#[macro_use]
@ -114,8 +107,9 @@ mod histbuf;
mod indexmap;
mod indexset;
mod linear_map;
pub mod storage;
pub mod string;
mod vec;
pub mod vec;
#[cfg(feature = "serde")]
mod de;

35
src/storage.rs Normal file
View File

@ -0,0 +1,35 @@
//! `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.
///
/// 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];
}

1690
src/vec.rs

File diff suppressed because it is too large Load Diff