1 unstable release

0.1.2 Nov 25, 2023
0.1.1 Nov 24, 2023
0.1.0 Nov 24, 2023

#2106 in Encoding

Download history 19/week @ 2024-07-26 3/week @ 2024-08-02 4/week @ 2024-09-06 48/week @ 2024-09-13 37/week @ 2024-09-20 22/week @ 2024-09-27 42/week @ 2024-10-04 53/week @ 2024-10-11 43/week @ 2024-10-18 42/week @ 2024-10-25 15/week @ 2024-11-01 60/week @ 2024-11-08

165 downloads per month

MIT license

12KB
200 lines

prost-stream

Read protobuf messages from a Stream

Examples

Stream

use prost_stream::Stream;
use std::net::TcpListener;
use std::net::TcpStream;

#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Ping {
    #[prost(uint64, tag = "1")]
    pub id: u64,
}

#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Pong {
    #[prost(uint64, tag = "1")]
    pub id: u64,
}

fn main() -> anyhow::Result<()> {    
    let listener = TcpListener::bind("127.0.0.1:0")?;
    let addr = listener.local_addr()?;

    std::thread::spawn(move || {
        let (stream, _) = listener.accept()?;
        let mut stream = Stream::new(stream);

        let _msg: Ping = stream.recv()?;
        stream.send(&Pong::default())?;

        anyhow::Result::<()>::Ok(())
    });

    let client = TcpStream::connect(addr)?;
    let mut client = Stream::new(client);

    client.send(&Ping::default())?;
    let pong: Pong = client.recv()?;

    assert_eq!(pong, Pong::default());

    Ok(())
}

AsyncStream

With async feature enabled, you can use AsyncStream

use prost_stream::AsyncStream;
use tokio::net::TcpListener;
use tokio::net::TcpStream;

#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Ping {
    #[prost(uint64, tag = "1")]
    pub id: u64,
}

#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Pong {
    #[prost(uint64, tag = "1")]
    pub id: u64,
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let listener = TcpListener::bind("127.0.0.1:0").await?;
    let addr = listener.local_addr()?;

    tokio::spawn(async move {
        let (stream, _) = listener.accept().await?;
        let mut stream = AsyncStream::new(stream);
        let _msg: Ping = stream.recv().await?;
        stream.send(&Pong::default()).await?;

        anyhow::Result::<()>::Ok(())
    });

    let client = TcpStream::connect(addr).await?;
    let mut client = AsyncStream::new(client);

    client.send(&Ping::default()).await?;
    let pong: Pong = client.recv().await?;

    assert_eq!(pong, Pong::default());

    Ok(())
}

Dependencies

~0.5–6.5MB
~41K SLoC