#buffer #networking #buf #vec #byte-buffer #read-write #io

netbuf

The simple to use, growable, contiguous buffer object with right assumptions and interface. Tuned for using it for network buffers.

15 releases

Uses old Rust 2015

0.4.1 Apr 14, 2018
0.4.0 Mar 18, 2017
0.3.8 Dec 12, 2016
0.3.7 Nov 24, 2016
0.1.0 Jul 24, 2015

#327 in Memory management

Download history 64/week @ 2023-12-11 68/week @ 2023-12-18 31/week @ 2023-12-25 13/week @ 2024-01-01 61/week @ 2024-01-08 37/week @ 2024-01-15 38/week @ 2024-01-22 17/week @ 2024-01-29 42/week @ 2024-02-05 53/week @ 2024-02-12 82/week @ 2024-02-19 112/week @ 2024-02-26 87/week @ 2024-03-04 111/week @ 2024-03-11 96/week @ 2024-03-18 110/week @ 2024-03-25

411 downloads per month
Used in 20 crates (8 directly)

MIT/Apache

40KB
867 lines

Netbuf

Documentation | Github | Crate

The network buffer for usage in asynchronous network applications. Has right interface for reading from network into the Buf and for parsing packets directly from it. It also has capacity management tuned for network application (i.e. it starts growing fast, but deallocated for idle connections).

License

Licensed under either of

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.


lib.rs:

Netbuf

Documentation | Github | Crate

This module currently includes single Buf struct for holding buffers. Comparing to Vec class buffer has different allocation policy and has a marker of consumed data (i.e. already processed by protocol parser or already written to socket)

The Buf is deemed good both for input and output network buffer.

It also contains helper methods read_from and write_to which are used to read and append bytes from stream that implements Read and write bytes from buffer to a stream which implements Write respectively.

Note there are basically three ways to fill the buffer:

  • Buf::read_from -- preallocates some chunk and gives it to object implemeting Read
  • Write::write -- writes chunk to buffer assuming more data will follow shortly, i.e. it does large preallocations
  • Buf::extend -- writes chunk to buffer assuming it will not grow in the near perspective, so it allocates minimum chunk to hold the data

In other words you should use:

  • Buf::read_from -- to read from the network
  • Write::write -- when you are constructing object directly to the buffer incrementally
  • Buf::extend -- when you put whole object in place and give it to the network code for sending

More documentation is found in Buf object itself

No runtime deps