#safe #async #net #tcp #async-networking #async-executor #network-communication

safina-net

Safe async network functions - ARCHIVED: Code moved to safina crate

10 releases

0.1.9 Oct 27, 2024
0.1.8 Apr 1, 2022
0.1.7 Mar 16, 2022
0.1.5 Feb 25, 2022
0.1.1 Dec 31, 2020

#1721 in Asynchronous

Download history 1/week @ 2024-07-15 23/week @ 2024-07-29 5/week @ 2024-08-26 12/week @ 2024-09-23 13/week @ 2024-09-30 2/week @ 2024-10-07 5/week @ 2024-10-21 137/week @ 2024-10-28

144 downloads per month

Apache-2.0

130KB
1.5K SLoC

ARCHIVED ARCHIVED ARCHIVED

This crate is archived and will not be updated.

The code is now at safina::net in the safina crate.


safina-net

This is a safe Rust library for network communication.

It is part of safina, a safe async runtime.

Features

  • forbid(unsafe_code)
  • Depends only on std
  • Good test coverage (91%)
  • Works with safina-executor or any async executor

Limitations

  • No AsyncRead or AsyncWrite implementations yet.
  • Unoptimized. Uses polling. This has more latency and CPU load than other async networking libraries.
  • TCP connect uses a blocking thread. Concurrent connect operations are limited by executor's blocking thread pool size.

Examples

let bind_addr =
    std::net::SocketAddr::from(([127, 0, 0, 1], 0));
let mut listener =
    safina_net::TcpListener::bind(&bind_addr)
    .unwrap();
let addr = listener.inner().local_addr().unwrap();
println!("{}", &addr);

let executor = safina_executor::Executor::default();
executor.spawn(async move {
    let mut tcp_stream =
        safina_net::TcpStream::connect(addr)
        .await
        .unwrap();
    let mut buf = String::new();
    tcp_stream.read_to_string(&mut buf).await.unwrap();
    println!("read {:?}", buf);
});

let (mut tcp_stream, _addr)
    = listener.accept().await.unwrap();
tcp_stream.write_all(b"response").await.unwrap();
println!("wrote response");

For complete examples, see the integration tests in tests/.

Alternatives

  • async-net
    • Dependencies are full of unsafe
  • async-io
    • Full of unsafe
  • tokio
    • Very popular
    • Fast
    • Internally incredibly complicated
    • Full of unsafe
  • tcp-stream
    • Blocks async executor threads
    • Contains a little unsafe code

Changelog

Changelog
  • v0.1.8 - Update docs.
  • v0.1.7 - Use safina-executor v0.3.1.
  • v0.1.6 - Use safina-executor v0.3.
  • V0.1.5 - Update docs.
  • v0.1.4 - Use safina-executor v0.2.
  • v0.1.3 - Increase test coverage
  • v0.1.2
    • Increase test coverage
    • Handle spurious EPROTOTYPE errors on macOS.
  • v0.1.1
    • Add methods to TcpStream: peek, read_vectored, flush, and write_vectored.
    • Support Windows
  • v0.1.0 - First published version

TO DO

  • Add additional crates with adapters to popular async io traits, like safina-net-tokio, safina-net-futures, etc.

Dependencies

~76KB