Skip to content

Commit 0704ae0

Browse files
authored
fix(tls): duplicated buffer init (#381)
1 parent 52b268d commit 0704ae0

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

compio-tls/src/stream/mod.rs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -111,30 +111,30 @@ impl<S> From<native_tls::TlsStream<SyncStream<S>>> for TlsStream<S> {
111111
}
112112
}
113113

114-
#[cfg(not(feature = "read_buf"))]
115-
#[inline]
116-
fn read_buf<B: IoBufMut>(reader: &mut impl io::Read, buf: &mut B) -> io::Result<usize> {
117-
let slice: &mut [MaybeUninit<u8>] = buf.as_mut_slice();
118-
slice.fill(MaybeUninit::new(0));
119-
let slice = unsafe { std::slice::from_raw_parts_mut(slice.as_mut_ptr().cast(), slice.len()) };
120-
reader.read(slice)
121-
}
122-
123-
#[cfg(feature = "read_buf")]
124-
#[inline]
125-
fn read_buf<B: IoBufMut>(reader: &mut impl io::Read, buf: &mut B) -> io::Result<usize> {
126-
let slice: &mut [MaybeUninit<u8>] = buf.as_mut_slice();
127-
let mut borrowed_buf = io::BorrowedBuf::from(slice);
128-
let mut cursor = borrowed_buf.unfilled();
129-
reader.read_buf(cursor.reborrow())?;
130-
Ok(cursor.written())
131-
}
132-
133114
impl<S: AsyncRead> AsyncRead for TlsStream<S> {
134115
async fn read<B: IoBufMut>(&mut self, mut buf: B) -> BufResult<usize, B> {
116+
let slice: &mut [MaybeUninit<u8>] = buf.as_mut_slice();
117+
118+
#[cfg(feature = "read_buf")]
119+
let mut f = {
120+
let mut borrowed_buf = io::BorrowedBuf::from(slice);
121+
move |s: &mut _| {
122+
let mut cursor = borrowed_buf.unfilled();
123+
std::io::Read::read_buf(s, cursor.reborrow())?;
124+
Ok::<usize, io::Error>(cursor.written())
125+
}
126+
};
127+
128+
#[cfg(not(feature = "read_buf"))]
129+
let mut f = {
130+
slice.fill(MaybeUninit::new(0));
131+
// SAFETY: The memory has been initialized
132+
let slice = unsafe { std::slice::from_raw_parts_mut(slice.as_mut_ptr().cast(), slice.len()) };
133+
|s: &mut _| std::io::Read::read(s, slice)
134+
};
135+
135136
loop {
136-
let res = read_buf(&mut self.0, &mut buf);
137-
match res {
137+
match f(&mut self.0) {
138138
Ok(res) => {
139139
unsafe { buf.set_buf_init(res) };
140140
return BufResult(Ok(res), buf);
@@ -145,7 +145,7 @@ impl<S: AsyncRead> AsyncRead for TlsStream<S> {
145145
Err(e) => return BufResult(Err(e), buf),
146146
}
147147
}
148-
_ => return BufResult(res, buf),
148+
res => return BufResult(res, buf),
149149
}
150150
}
151151
}

0 commit comments

Comments
 (0)