Skip to content

Commit 76e6419

Browse files
committed
Remove GAT in Buf trait
Due to rust-lang/rust-analyzer#19502 the GAT in Buf renders autocomplete useless in methods generic over Buf.
1 parent 2ea4b7e commit 76e6419

File tree

2 files changed

+23
-19
lines changed

2 files changed

+23
-19
lines changed

cryprot-core/src/buf.rs

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,26 @@ use crate::alloc::{HugePageMemory, allocate_zeroed_vec};
1414
pub 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

5157
impl<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

8186
impl<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
}

cryprot-ot/src/adapter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ where
184184
B: Buf<Block>,
185185
F: FnMut(usize) -> Block + Send,
186186
{
187-
let mut r_ots: B::BufKind<[Block; 2]> = B::BufKind::zeroed(ots.len());
187+
let mut r_ots = B::zeroed_arr2(ots.len());
188188
self.0.send_into(&mut r_ots).await?;
189189
let mut send_buf: Vec<Block> = Vec::zeroed(COR_CHUNK_SIZE);
190190
let (mut tx, _) = self.connection().byte_stream().await?;

0 commit comments

Comments
 (0)