Skip to content

Commit e9f8bac

Browse files
committed
vec: remove code duplication due to VecView.
1 parent 1f25e65 commit e9f8bac

File tree

3 files changed

+443
-1036
lines changed

3 files changed

+443
-1036
lines changed

src/lib.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,6 @@ pub use indexmap::{
9696
pub use indexset::{FnvIndexSet, IndexSet, Iter as IndexSetIter};
9797
pub use linear_map::LinearMap;
9898
pub use string::String;
99-
100-
// Workaround https://github.com/rust-lang/rust/issues/119015. This is required so that the methods on `VecView` and `Vec` are properly documented.
101-
// cfg(doc) prevents `VecInner` being part of the public API.
102-
// doc(hidden) prevents the `pub use vec::VecInner` from being visible in the documentation.
103-
#[cfg(doc)]
104-
#[doc(hidden)]
105-
pub use vec::VecInner as _;
10699
pub use vec::{Vec, VecView};
107100

108101
#[macro_use]
@@ -114,8 +107,9 @@ mod histbuf;
114107
mod indexmap;
115108
mod indexset;
116109
mod linear_map;
110+
pub mod storage;
117111
pub mod string;
118-
mod vec;
112+
pub mod vec;
119113

120114
#[cfg(feature = "serde")]
121115
mod de;

src/storage.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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.
17+
///
18+
/// This trait is sealed, so you cannot implement it for your own types. You can only use
19+
/// the implementations provided by this crate.
20+
#[allow(private_bounds)]
21+
pub trait Storage: SealedStorage {}
22+
23+
/// Implementation of [`Storage`] that stores the data in an array `[T; N]` whose size is known at compile time.
24+
pub enum OwnedStorage<const N: usize> {}
25+
impl<const N: usize> Storage for OwnedStorage<N> {}
26+
impl<const N: usize> SealedStorage for OwnedStorage<N> {
27+
type Buffer<T> = [T; N];
28+
}
29+
30+
/// Implementation of [`Storage`] that stores the data in an unsized `[T]`.
31+
pub enum ViewStorage {}
32+
impl Storage for ViewStorage {}
33+
impl SealedStorage for ViewStorage {
34+
type Buffer<T> = [T];
35+
}

0 commit comments

Comments
 (0)