Skip to content

Commit

Permalink
Add examples for signals and fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
joshka committed Dec 10, 2024
1 parent 3e51766 commit 75bb441
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 2 deletions.
2 changes: 1 addition & 1 deletion tokio-stream/src/wrappers/read_dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use tokio::fs::{DirEntry, ReadDir};
///
/// ```
/// use tokio::fs::read_dir;
/// use tokio_stream::{wrappers::ReadDirStream, StreamExt};
/// use tokio_stream::{StreamExt, wrappers::ReadDirStream};
///
/// # #[tokio::main]
/// # async fn main() -> std::io::Result<()> {
Expand Down
16 changes: 16 additions & 0 deletions tokio-stream/src/wrappers/signal_unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,22 @@ use tokio::signal::unix::Signal;

/// A wrapper around [`Signal`] that implements [`Stream`].
///
/// # Example
///
/// ```no_run
/// use tokio::signal::unix::{signal, SignalKind};
/// use tokio_stream::{StreamExt, wrappers::SignalStream};
///
/// # #[tokio::main]
/// # async fn main() -> std::io::Result<()> {
/// let signals = signal(SignalKind::hangup())?;
/// let mut stream = SignalStream::new(signals);
/// while stream.next().await.is_some() {
/// println!("hangup signal received");
/// }
/// # Ok(())
/// # }
/// ```
/// [`Signal`]: struct@tokio::signal::unix::Signal
/// [`Stream`]: trait@crate::Stream
#[derive(Debug)]
Expand Down
34 changes: 34 additions & 0 deletions tokio-stream/src/wrappers/signal_windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,23 @@ use tokio::signal::windows::{CtrlBreak, CtrlC};
///
/// [`CtrlC`]: struct@tokio::signal::windows::CtrlC
/// [`Stream`]: trait@crate::Stream
///
/// # Example
///
/// ```no_run
/// use tokio::signal::windows::ctrl_c;
/// use tokio_stream::{StreamExt, wrappers::CtrlCStream};
///
/// # #[tokio::main]
/// # async fn main() -> std::io::Result<()> {
/// let signals = ctrl_c()?;
/// let mut stream = CtrlCStream::new();
/// while stream.next().await.is_some() {
/// println!("ctrl-c received");
/// }
/// # Ok(())
/// # }
/// ```
#[derive(Debug)]
#[cfg_attr(docsrs, doc(cfg(all(windows, feature = "signal"))))]
pub struct CtrlCStream {
Expand Down Expand Up @@ -47,6 +64,23 @@ impl AsMut<CtrlC> for CtrlCStream {

/// A wrapper around [`CtrlBreak`] that implements [`Stream`].
///
/// # Example
///
/// ```no_run
/// use tokio::signal::windows::ctrl_break;
/// use tokio_stream::{StreamExt, wrappers::CtrlBreakStream};
///
/// # #[tokio::main]
/// # async fn main() -> std::io::Result<()> {
/// let signals = ctrl_break()?;
/// let mut stream = CtrlBreakStream::new(signals);
/// while stream.next().await.is_some() {
/// println!("ctrl-break received");
/// }
/// # Ok(())
/// # }
/// ```
///
/// [`CtrlBreak`]: struct@tokio::signal::windows::CtrlBreak
/// [`Stream`]: trait@crate::Stream
#[derive(Debug)]
Expand Down
18 changes: 18 additions & 0 deletions tokio-stream/src/wrappers/split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,24 @@ use tokio::io::{AsyncBufRead, Split};
pin_project! {
/// A wrapper around [`tokio::io::Split`] that implements [`Stream`].
///
/// # Example
///
/// ```
/// use tokio::io::AsyncBufReadExt;
/// use tokio_stream::{StreamExt, wrappers::SplitStream};
///
/// # #[tokio::main]
/// # async fn main() -> std::io::Result<()> {
/// let input = "Hello\nWorld\n".as_bytes();
/// let lines = AsyncBufReadExt::split(input, b'\n');
///
/// let mut stream = SplitStream::new(lines);
/// while let Some(line) = stream.next().await {
/// println!("length = {}", line?.len())
/// }
/// # Ok(())
/// # }
/// ```
/// [`tokio::io::Split`]: struct@tokio::io::Split
/// [`Stream`]: trait@crate::Stream
#[derive(Debug)]
Expand Down
2 changes: 1 addition & 1 deletion tokio-stream/src/wrappers/tcp_listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use tokio::net::{TcpListener, TcpStream};
/// use std::net::{Ipv4Addr, Ipv6Addr};
///
/// use tokio::net::TcpListener;
/// use tokio_stream::{wrappers::TcpListenerStream, StreamExt};
/// use tokio_stream::{StreamExt, wrappers::TcpListenerStream};
///
/// # #[tokio::main]
/// # async fn main() -> std::io::Result<()> {
Expand Down
19 changes: 19 additions & 0 deletions tokio-stream/src/wrappers/unix_listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,25 @@ use tokio::net::{UnixListener, UnixStream};

/// A wrapper around [`UnixListener`] that implements [`Stream`].
///
/// # Example
///
/// ```no_run
/// use tokio::net::UnixListener;
/// use tokio_stream::{StreamExt, wrappers::UnixListenerStream};
///
/// # #[tokio::main]
/// # async fn main() -> std::io::Result<()> {
/// let listener = UnixListener::bind("/tmp/sock")?;
/// let mut incoming = UnixListenerStream::new(listener);
///
/// while let Some(stream) = incoming.next().await {
/// let stream = stream?;
/// let peer_addr = stream.peer_addr()?;
/// println!("Accepted connection from: {peer_addr:?}");
/// }
/// # Ok(())
/// # }
/// ```
/// [`UnixListener`]: struct@tokio::net::UnixListener
/// [`Stream`]: trait@crate::Stream
#[derive(Debug)]
Expand Down

0 comments on commit 75bb441

Please sign in to comment.