#tokio #peek #future #async-io

peekable

Peekable reader and async reader, which enhance your network programming experience

4 releases

0.3.0 Feb 21, 2025
0.2.4 Dec 20, 2024
0.2.3 Mar 21, 2024
0.2.1 Feb 9, 2024
0.1.0 Jan 24, 2024

#253 in Asynchronous

Download history 15/week @ 2024-12-01 21/week @ 2024-12-08 140/week @ 2024-12-15 1191/week @ 2024-12-22 968/week @ 2024-12-29 225/week @ 2025-01-05 258/week @ 2025-01-12 532/week @ 2025-01-19 757/week @ 2025-01-26 608/week @ 2025-02-02 775/week @ 2025-02-09 1275/week @ 2025-02-16 1464/week @ 2025-02-23 2039/week @ 2025-03-02 1045/week @ 2025-03-09 837/week @ 2025-03-16

5,864 downloads per month
Used in 10 crates (4 directly)

MIT/Apache

98KB
1.5K SLoC

Peekable

Peekable reader and async reader, which enhance your network programming experience.

github LoC Build codecov

docs.rs crates.io crates.io license

English | 简体中文

Installation

  • Std

    [dependencies]
    peekable = "0.3"
    
  • Tokio I/O

    [dependencies]
    peekable = { version = "0.3", features = ["tokio"] }
    
  • Futures I/O

    [dependencies]
    peekable = { version = "0.3", features = ["future"] }
    

Examples

  • Std

    use std::{net::TcpStream, io::Read};
    use peekable::Peekable;
    
    let conn = TcpStream::connect("127.0.0.1:8080").unwrap();
    let mut peekable = Peekable::from(conn);
    
    let mut peeked = [0; 16];
    peekable.peek_exact(&mut peeked).unwrap();
    
    let mut readed = [0; 16];
    peekable.read_exact(&mut readed).unwrap();
    
    assert_eq!(peeked, readed);
    
  • Tokio I/O

    use tokio::{net::TcpStream, io::AsyncReadExt};
    use peekable::tokio::AsyncPeekable;
    
    let conn = TcpStream::connect("127.0.0.1:8080").await.unwrap();
    let mut peekable = AsyncPeekable::from(conn);
    
    let mut peeked = [0; 16];
    peekable.peek_exact(&mut peeked).await.unwrap();
    
    let mut readed = [0; 16];
    peekable.read_exact(&mut readed).await.unwrap();
    
    assert_eq!(peeked, readed);
    
  • Futures I/O

    use async_std::net::TcpStream;
    use futures::AsyncReadExt;
    use peekable::future::AsyncPeekable;
    
    let conn = TcpStream::connect("127.0.0.1:8080").await.unwrap();
    let mut peekable = AsyncPeekable::from(conn);
    
    let mut peeked = [0; 16];
    peekable.peek_exact(&mut peeked).await.unwrap();
    
    let mut readed = [0; 16];
    peekable.read_exact(&mut readed).await.unwrap();
    
    assert_eq!(peeked, readed);
    

License

peekable is under the terms of both the MIT license and the Apache License (Version 2.0).

See LICENSE-APACHE, LICENSE-MIT for details.

Copyright (c) 2024 Al Liu.

Dependencies

~0–6MB
~25K SLoC