Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,19 @@ use byte_pool::BytePool;
// Create a pool
let pool = BytePool::<Vec<u8>>::new();

// Allocate a buffer
let mut buf = pool.alloc(1024);
// Allocate and copy from existing bytes.
let payload = pool.alloc_from_slice(b"hello");
assert_eq!(&payload[..], b"hello");

// write some data into it
for i in 0..100 {
buf[i] = 12;
}

// Check that we actually wrote sth.
assert_eq!(buf[55], 12);
// Allocate a writable frame and shrink it after recv/write.
let mut frame = pool.alloc_and_fill(1024);
let received = 128;
frame.set_filled_len(received);
assert_eq!(frame.len(), received);

// Returns the underlying memory to the pool.
drop(buf);
drop(payload);
drop(frame);

// Frees all memory in the pool.
drop(pool);
Expand Down
20 changes: 10 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@
//! // Create a pool
//! let pool = BytePool::<Vec<u8>>::new();
//!
//! // Allocate a buffer with capacity 1024.
//! let mut buf = pool.alloc(1024);
//! // Allocate and copy from existing bytes.
//! let payload = pool.alloc_from_slice(b"hello");
//! assert_eq!(&payload[..], b"hello");
//!
//! // write some data into it
//! for i in 0..100 {
//! buf[i] = 12;
//! }
//!
//! // Check that we actually wrote sth.
//! assert_eq!(buf[55], 12);
//! // Allocate a writable frame and then shrink to received size.
//! let mut frame = pool.alloc_and_fill(1024);
//! let received = 128;
//! frame.set_filled_len(received);
//! assert_eq!(frame.len(), received);
//!
//! // Returns the underlying memory to the pool.
//! drop(buf);
//! drop(payload);
//! drop(frame);
//!
//! // Frees all memory in the pool.
//! drop(pool);
Expand Down
Loading