-
Notifications
You must be signed in to change notification settings - Fork 336
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add BufRead::fill_buf * Make FillBufFuture constructor pub(crate) * Give more information about the transmutation source type
- Loading branch information
Showing
2 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
use std::pin::Pin; | ||
|
||
use futures_io::AsyncBufRead; | ||
|
||
use crate::future::Future; | ||
use crate::io; | ||
use crate::task::{Context, Poll}; | ||
|
||
#[doc(hidden)] | ||
#[allow(missing_debug_implementations)] | ||
pub struct FillBufFuture<'a, R: ?Sized> { | ||
reader: &'a mut R, | ||
} | ||
|
||
impl<'a, R: ?Sized> FillBufFuture<'a, R> { | ||
pub(crate) fn new(reader: &'a mut R) -> Self { | ||
Self { reader } | ||
} | ||
} | ||
|
||
impl<'a, R: AsyncBufRead + Unpin + ?Sized> Future for FillBufFuture<'a, R> { | ||
type Output = io::Result<&'a [u8]>; | ||
|
||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<&'a [u8]>> { | ||
let Self { reader } = &mut *self; | ||
let result = Pin::new(reader).poll_fill_buf(cx); | ||
// This is safe because: | ||
// 1. The buffer is valid for the lifetime of the reader. | ||
// 2. Output is unrelated to the wrapper (Self). | ||
result.map_ok(|buf| unsafe { std::mem::transmute::<&'_ [u8], &'a [u8]>(buf) }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters