Skip to content

Commit

Permalink
nit: use Option<i32> instead of Option<u8> for compression level (#308)
Browse files Browse the repository at this point in the history
  • Loading branch information
danlaine authored Jan 4, 2025
1 parent 0dc20c9 commit b22b4b9
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 10 deletions.
2 changes: 1 addition & 1 deletion examples/chat/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ fn main() {

// Initialize chat
const MAX_MESSAGE_BACKLOG: usize = 128;
const COMPRESSION_LEVEL: Option<u8> = Some(3);
const COMPRESSION_LEVEL: Option<i32> = Some(3);
let (chat_sender, chat_receiver) = network.register(
handler::CHANNEL,
Quota::per_second(NonZeroU32::new(128).unwrap()),
Expand Down
2 changes: 1 addition & 1 deletion examples/vrf/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ fn main() {

// Check if I am the arbiter
const DEFAULT_MESSAGE_BACKLOG: usize = 256;
const COMPRESSION_LEVEL: Option<u8> = Some(3);
const COMPRESSION_LEVEL: Option<i32> = Some(3);
if let Some(arbiter) = matches.get_one::<u64>("arbiter") {
// Create contributor
let rogue = matches.get_flag("rogue");
Expand Down
9 changes: 4 additions & 5 deletions p2p/src/authenticated/channels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ use zstd::bulk::{compress, decompress};
pub struct Sender {
channel: Channel,
max_size: usize,
compression: Option<u8>,
compression: Option<i32>,
messenger: Messenger,
}

impl Sender {
pub(super) fn new(
channel: Channel,
max_size: usize,
compression: Option<u8>,
compression: Option<i32>,
messenger: Messenger,
) -> Self {
Self {
Expand Down Expand Up @@ -63,8 +63,7 @@ impl crate::Sender for Sender {
) -> Result<Vec<PublicKey>, Error> {
// If compression is enabled, compress the message before sending.
if let Some(level) = self.compression {
let compressed =
compress(&message, level as i32).map_err(|_| Error::CompressionFailed)?;
let compressed = compress(&message, level).map_err(|_| Error::CompressionFailed)?;
message = compressed.into();
}

Expand Down Expand Up @@ -148,7 +147,7 @@ impl Channels {
channel: Channel,
rate: governor::Quota,
backlog: usize,
compression: Option<u8>,
compression: Option<i32>,
) -> (Sender, Receiver) {
let (sender, receiver) = mpsc::channel(backlog);
if self.receivers.insert(channel, (rate, sender)).is_some() {
Expand Down
4 changes: 2 additions & 2 deletions p2p/src/authenticated/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@
//!
//! // Register some channel
//! const MAX_MESSAGE_BACKLOG: usize = 128;
//! const COMPRESSION_LEVEL: Option<u8> = Some(3);
//! const COMPRESSION_LEVEL: Option<i32> = Some(3);
//! let (sender, receiver) = network.register(
//! 0,
//! Quota::per_second(NonZeroU32::new(1).unwrap()),
Expand Down Expand Up @@ -548,7 +548,7 @@ mod tests {
});
}

fn test_message_too_large(compression: Option<u8>) {
fn test_message_too_large(compression: Option<i32>) {
// Configure test
let base_port = 3000;
let n: usize = 2;
Expand Down
2 changes: 1 addition & 1 deletion p2p/src/authenticated/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ impl<
channel: Channel,
rate: Quota,
backlog: usize,
compression: Option<u8>,
compression: Option<i32>,
) -> (channels::Sender, channels::Receiver) {
self.channels.register(channel, rate, backlog, compression)
}
Expand Down

0 comments on commit b22b4b9

Please sign in to comment.