Skip to content

Commit

Permalink
new: use non zero usize type for buffer size and max message size
Browse files Browse the repository at this point in the history
  • Loading branch information
pamburus committed Jun 7, 2022
1 parent 53020b6 commit 17f7e63
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
11 changes: 6 additions & 5 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crossbeam_channel::RecvError;
use crossbeam_utils::thread;
use itertools::izip;
use serde_json as json;
use std::num::NonZeroUsize;

use crate::datefmt::{DateTimeFormat, DateTimeFormatter};
use crate::error::*;
Expand All @@ -23,8 +24,8 @@ pub struct Options {
pub theme: Arc<Theme>,
pub time_format: DateTimeFormat,
pub raw_fields: bool,
pub buffer_size: usize,
pub max_message_size: usize,
pub buffer_size: NonZeroUsize,
pub max_message_size: NonZeroUsize,
pub concurrency: usize,
pub filter: Filter,
pub fields: FieldOptions,
Expand Down Expand Up @@ -52,8 +53,8 @@ impl App {
output: &mut (dyn Write + Send + Sync),
) -> Result<()> {
let n = self.options.concurrency;
let sfi = Arc::new(SegmentBufFactory::new(self.options.buffer_size));
let bfo = BufFactory::new(self.options.buffer_size);
let sfi = Arc::new(SegmentBufFactory::new(self.options.buffer_size.into()));
let bfo = BufFactory::new(self.options.buffer_size.into());
let parser = Parser::new(ParserSettings::new(
&self.options.fields.settings,
self.options.filter.since.is_some() || self.options.filter.until.is_some(),
Expand All @@ -70,7 +71,7 @@ impl App {
let reader = scope.spawn(closure!(clone sfi, |_| -> Result<()> {
let mut sn: usize = 0;
let scanner = Scanner::new(sfi, "\n".to_string());
for item in scanner.items(input).with_max_segment_size(self.options.max_message_size) {
for item in scanner.items(input).with_max_segment_size(self.options.max_message_size.into()) {
if let Err(_) = txi[sn % n].send(item?) {
break;
}
Expand Down
14 changes: 7 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use clap::{ArgEnum, Parser};
use itertools::Itertools;
use once_cell::sync::Lazy;
use platform_dirs::AppDirs;
use std::num::NonZeroUsize;

// local imports
use hl::datefmt::LinuxDateFormat;
Expand Down Expand Up @@ -88,11 +89,11 @@ struct Opt {
//
/// Buffer size.
#[clap(long, default_value = "2 MiB", env="HL_BUFFER_SIZE", overrides_with = "buffer-size", parse(try_from_str = parse_non_zero_size))]
buffer_size: usize,
buffer_size: NonZeroUsize,
//
/// Maximum message size.
#[clap(long, default_value = "64 MiB", env="HL_MAX_MESSAGE_SIZE", overrides_with = "max-message-size", parse(try_from_str = parse_non_zero_size))]
max_message_size: usize,
max_message_size: NonZeroUsize,
//
/// Number of processing threads.
#[clap(
Expand Down Expand Up @@ -204,12 +205,11 @@ fn parse_size(s: &str) -> std::result::Result<usize, SizeParseError> {
}
}

fn parse_non_zero_size(s: &str) -> std::result::Result<usize, NonZeroSizeParseError> {
let value = parse_size(s)?;
if value == 0 {
Err(NonZeroSizeParseError::ZeroSize)
fn parse_non_zero_size(s: &str) -> std::result::Result<NonZeroUsize, NonZeroSizeParseError> {
if let Some(value) = NonZeroUsize::new(parse_size(s)?) {
Ok(NonZeroUsize::from(value))
} else {
Ok(value)
Err(NonZeroSizeParseError::ZeroSize)
}
}

Expand Down

0 comments on commit 17f7e63

Please sign in to comment.