forked from GGist/bip-rs
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
f889ad6 deps: update umio to mio v1 (Cameron Garnham) e952875 deps: add umio as contrib package (Cameron Garnham) 0e26eb7 docs: update main readme todo (Cameron Garnham) 066008d rework: upgrade all dependencies (Cameron Garnham) Pull request description: closes: #19, closes: #20, closes #21, closes: #22, closes: #23, closes: #25 Todo: - [x] Update Readme ACKs for top commit: da2ce7: ACK f889ad6 Tree-SHA512: 21ee2614de6df9dc3669d14512d1fdae2258e5368624fcec3624b7f128816756c66076d6983758689235b0466da2c2decbf0049ba75180492902df82646d9933
- Loading branch information
Showing
189 changed files
with
11,146 additions
and
7,645 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
[workspace] | ||
members = [ | ||
"contrib/umio", | ||
"examples/get_metadata", | ||
"examples/simple_torrent", | ||
"packages/bencode", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
[package] | ||
authors = ["Andrew <[email protected]>"] | ||
description = "Message Based Readiness API In Rust" | ||
keywords = ["message", "mio", "readyness"] | ||
name = "umio" | ||
readme = "README.md" | ||
|
||
categories.workspace = true | ||
documentation.workspace = true | ||
edition.workspace = true | ||
homepage.workspace = true | ||
license.workspace = true | ||
publish.workspace = true | ||
|
||
repository.workspace = true | ||
version.workspace = true | ||
|
||
[dependencies] | ||
mio = { version = "1", features = ["net", "os-poll"] } | ||
tracing = "0" | ||
|
||
[dev-dependencies] | ||
tracing-subscriber = "0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
umio-rs | ||
======= | ||
Message Based Readiness API In Rust. | ||
|
||
Thin layer over mio for working with a single udp socket while retaining access to timers and event loop channels. | ||
|
||
|
||
License | ||
------- | ||
|
||
Licensed under either of | ||
|
||
* Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) | ||
* MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) | ||
|
||
at your option. | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
use std::ops::{Deref, DerefMut}; | ||
|
||
use tracing::instrument; | ||
|
||
#[allow(clippy::module_name_repetitions)] | ||
pub struct BufferPool { | ||
// Use Stack For Temporal Locality | ||
buffers: Vec<Buffer>, | ||
buffer_size: usize, | ||
} | ||
|
||
impl std::fmt::Debug for BufferPool { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
f.debug_struct("BufferPool") | ||
.field("buffers_len", &self.buffers.len()) | ||
.field("buffer_size", &self.buffer_size) | ||
.finish() | ||
} | ||
} | ||
|
||
impl BufferPool { | ||
#[instrument(skip())] | ||
pub fn new(buffer_size: usize) -> BufferPool { | ||
let buffers = Vec::new(); | ||
|
||
BufferPool { buffers, buffer_size } | ||
} | ||
|
||
#[instrument(skip(self), fields(remaining= %self.buffers.len()))] | ||
pub fn pop(&mut self) -> Buffer { | ||
if let Some(buffer) = self.buffers.pop() { | ||
tracing::trace!(?buffer, "popping old buffer taken from pool"); | ||
buffer | ||
} else { | ||
let buffer = Buffer::new(self.buffer_size); | ||
tracing::trace!(?buffer, "creating new buffer..."); | ||
buffer | ||
} | ||
} | ||
|
||
#[instrument(skip(self, buffer), fields(existing= %self.buffers.len()))] | ||
pub fn push(&mut self, mut buffer: Buffer) { | ||
tracing::trace!("Pushing buffer back to pool"); | ||
buffer.reset_position(); | ||
self.buffers.push(buffer); | ||
} | ||
} | ||
|
||
//----------------------------------------------------------------------------// | ||
|
||
/// Reusable region of memory for incoming and outgoing messages. | ||
pub struct Buffer { | ||
buffer: std::io::Cursor<Vec<u8>>, | ||
} | ||
|
||
impl std::fmt::Debug for Buffer { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
f.debug_struct("Buffer").field("buffer", &self.as_ref()).finish() | ||
} | ||
} | ||
|
||
impl Buffer { | ||
#[instrument(skip())] | ||
fn new(len: usize) -> Buffer { | ||
Buffer { | ||
buffer: std::io::Cursor::new(vec![0_u8; len]), | ||
} | ||
} | ||
|
||
fn reset_position(&mut self) { | ||
self.set_position(0); | ||
} | ||
} | ||
|
||
impl Deref for Buffer { | ||
type Target = std::io::Cursor<Vec<u8>>; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
&self.buffer | ||
} | ||
} | ||
|
||
impl DerefMut for Buffer { | ||
fn deref_mut(&mut self) -> &mut Self::Target { | ||
&mut self.buffer | ||
} | ||
} | ||
|
||
impl AsRef<[u8]> for Buffer { | ||
fn as_ref(&self) -> &[u8] { | ||
self.get_ref().split_at(self.buffer.position().try_into().unwrap()).0 | ||
} | ||
} | ||
|
||
impl AsMut<[u8]> for Buffer { | ||
fn as_mut(&mut self) -> &mut [u8] { | ||
let pos = self.buffer.position().try_into().unwrap(); | ||
self.get_mut().split_at_mut(pos).1 | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
|
||
use super::{Buffer, BufferPool}; | ||
|
||
const DEFAULT_BUFFER_SIZE: usize = 1500; | ||
|
||
#[test] | ||
fn positive_buffer_pool_buffer_len() { | ||
let mut buffers = BufferPool::new(DEFAULT_BUFFER_SIZE); | ||
let mut buffer = buffers.pop(); | ||
|
||
assert_eq!(buffer.as_mut().len(), DEFAULT_BUFFER_SIZE); | ||
assert_eq!(buffer.as_ref().len(), 0); | ||
} | ||
|
||
#[test] | ||
fn positive_buffer_len_update() { | ||
let mut buffer = Buffer::new(DEFAULT_BUFFER_SIZE); | ||
|
||
buffer.set_position((DEFAULT_BUFFER_SIZE - 1).try_into().unwrap()); | ||
|
||
assert_eq!(buffer.as_mut().len(), 1); | ||
assert_eq!(buffer.as_ref().len(), DEFAULT_BUFFER_SIZE - 1); | ||
} | ||
} |
Oops, something went wrong.