@@ -14,20 +14,26 @@ use crate::alloc::{HugePageMemory, allocate_zeroed_vec};
1414pub trait Buf < T > :
1515 Default + Debug + Deref < Target = [ T ] > + DerefMut + Send + Sync + ' static + private:: Sealed
1616{
17- /// The 'kind' of this Buf implementation. This is e.g. `Vec<E>` for `Vec<T>
18- /// as Buf<T>`.
19- ///
20- /// This associated type can be used to create Bufs of the same kind, but
21- /// with a different inner type.
22- type BufKind < E > : Buf < E >
23- where
24- E : Zeroable + Clone + Default + Debug + Send + Sync + ' static ;
2517 /// Create a new `Buf` of length `len` with all elements set to zero.
2618 ///
2719 /// Implementations of this directly allocate zeroed memory and do not write
2820 /// zeroes to the elements explicitly.
2921 fn zeroed ( len : usize ) -> Self ;
3022
23+ /// Create a new [`Buf`] of the same kind but for two-element arrays of
24+ /// `T``.
25+ ///
26+ /// This method is useful in methods generic over Buf which need a temporary
27+ /// buffer over arrays of size two of `T`, that is the same kind of buffer
28+ /// the method is called with. That is, a `fn foo(b: impl Buf<T>)`
29+ /// called with a [`HugePageMemory`] can use this method to create a
30+ /// `HugePageMemory<[T;2]>`.
31+ //
32+ // Note: Ideally we would use a GAT on Buf for this, but sadly due to
33+ // https://github.com/rust-lang/rust-analyzer/issues/19502 in rust-analyzer,
34+ // this renders autocomplete unusable in methods generic over Buf.
35+ fn zeroed_arr2 ( len : usize ) -> impl Buf < [ T ; 2 ] > ;
36+
3137 /// Capacity of the `Buf`.
3238 fn capacity ( & self ) -> usize ;
3339
@@ -49,15 +55,14 @@ pub trait Buf<T>:
4955}
5056
5157impl < T : Zeroable + Clone + Default + Debug + Send + Sync + ' static > Buf < T > for Vec < T > {
52- type BufKind < E >
53- = Vec < E >
54- where
55- E : Zeroable + Clone + Default + Debug + Send + Sync + ' static ;
56-
5758 fn zeroed ( len : usize ) -> Self {
5859 allocate_zeroed_vec ( len)
5960 }
6061
62+ fn zeroed_arr2 ( len : usize ) -> impl Buf < [ T ; 2 ] > {
63+ allocate_zeroed_vec ( len)
64+ }
65+
6166 fn capacity ( & self ) -> usize {
6267 self . capacity ( )
6368 }
@@ -79,15 +84,14 @@ impl<T: Zeroable + Clone + Default + Debug + Send + Sync + 'static> Buf<T> for V
7984}
8085
8186impl < T : Zeroable + Clone + Default + Debug + Send + Sync + ' static > Buf < T > for HugePageMemory < T > {
82- type BufKind < E >
83- = HugePageMemory < E >
84- where
85- E : Zeroable + Clone + Default + Debug + Send + Sync + ' static ;
86-
8787 fn zeroed ( len : usize ) -> Self {
8888 HugePageMemory :: zeroed ( len)
8989 }
9090
91+ fn zeroed_arr2 ( len : usize ) -> impl Buf < [ T ; 2 ] > {
92+ HugePageMemory :: zeroed ( len)
93+ }
94+
9195 fn capacity ( & self ) -> usize {
9296 self . capacity ( )
9397 }
0 commit comments