forked from tokio-rs/tokio
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dae1fb8
commit 7f02930
Showing
2 changed files
with
17 additions
and
46 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
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 |
---|---|---|
@@ -1,52 +1,27 @@ | ||
#![warn(rust_2018_idioms)] | ||
|
||
use futures_util::SinkExt; | ||
use std::fmt::{self, Debug, Display}; | ||
use std::io::{self, Error, ErrorKind}; | ||
use tokio::io::AsyncWriteExt; | ||
use tokio_util::io::SinkWriter; | ||
use tokio_util::sync::{PollSendError, PollSender}; | ||
|
||
#[derive(Debug)] | ||
struct PollSendErrorCoupler<T>(PollSendError<T>); | ||
|
||
impl<T: Debug> Display for PollSendErrorCoupler<T> { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
Display::fmt(&self, f) | ||
} | ||
} | ||
|
||
impl<T: Debug> std::error::Error for PollSendErrorCoupler<T> {} | ||
|
||
impl<T> From<PollSendError<T>> for PollSendErrorCoupler<T> { | ||
fn from(e: PollSendError<T>) -> Self { | ||
PollSendErrorCoupler(e) | ||
} | ||
} | ||
impl<T> Into<io::Error> for PollSendErrorCoupler<T> { | ||
fn into(self) -> io::Error { | ||
io::Error::from(ErrorKind::BrokenPipe) | ||
} | ||
// add code here | ||
} | ||
use tokio_util::sync::PollSender; | ||
|
||
#[tokio::test] | ||
async fn test_sink_writer() -> Result<(), Error> { | ||
// Construct a channel pair to send data across and wrap a pollable sink. | ||
// Note that the sink must mimic a writable object, e.g. have `std::io::Error` | ||
// as its error type. | ||
let (tx, mut rx) = tokio::sync::mpsc::channel::<&[u8]>(1); | ||
let writer = SinkWriter::new( | ||
let (tx, mut rx) = tokio::sync::mpsc::channel::<Vec<u8>>(1); | ||
let mut writer = SinkWriter::new( | ||
PollSender::new(tx).sink_map_err(|_| io::Error::from(ErrorKind::BrokenPipe)), | ||
); | ||
|
||
// Write data to our interface... | ||
|
||
let data: [u8; 4] = [1, 2, 3, 4]; | ||
//let _ = writer.write(&data); | ||
let _ = writer.write(&data).await; | ||
|
||
// ... and receive it. | ||
assert_eq!(&data, rx.recv().await.unwrap()); | ||
assert_eq!(data.to_vec(), rx.recv().await.unwrap()); | ||
|
||
Ok(()) | ||
} |