Skip to content

Commit

Permalink
Rename HasLength to RangeLength and move it under 'util'.
Browse files Browse the repository at this point in the history
  • Loading branch information
martinling authored and miek committed Oct 31, 2024
1 parent f937a53 commit e72d11a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 21 deletions.
3 changes: 2 additions & 1 deletion src/capture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ use crate::database::{
};
use crate::usb::{self, prelude::*};
use crate::util::{
id::{Id, HasLength},
id::Id,
rcu::SingleWriterRcu,
vec_map::{Key, VecMap},
Bytes,
RangeLength,
fmt_count,
fmt_size,
};
Expand Down
5 changes: 1 addition & 4 deletions src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ use crate::capture::{
INVALID_EP_ID,
};
use crate::usb::{self, prelude::*, validate_packet};
use crate::util::{
id::HasLength,
Bytes, fmt_count, fmt_size, titlecase
};
use crate::util::{Bytes, RangeLength, fmt_count, fmt_size, titlecase};

pub trait ItemSource<Item, ViewMode> {
/// Fetch an item from the source by index, relative to either the root
Expand Down
16 changes: 0 additions & 16 deletions src/util/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,6 @@ impl<T> Debug for Id<T> {
}
}

pub trait HasLength {
fn len(&self) -> u64;
}

impl<T> HasLength for Range<Id<T>> {
fn len(&self) -> u64 {
self.end.value - self.start.value
}
}

impl HasLength for Range<u64> {
fn len(&self) -> u64 {
self.end - self.start
}
}

impl<T> PartialEq<Id<T>> for Id<T> {
fn eq(&self, other: &Id<T>) -> bool {
self.value.eq(&other.value)
Expand Down
20 changes: 20 additions & 0 deletions src/util/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Utility code that doesn't belong anywhere specific.

use std::ops::Range;

use anyhow::{Error, bail};
use num_format::{Locale, ToFormattedString};
use humansize::{SizeFormatter, BINARY};
Expand All @@ -9,6 +11,8 @@ pub mod id;
pub mod vec_map;
pub mod rcu;

use id::Id;

pub fn fmt_count(count: u64) -> String {
count.to_formatted_string(&Locale::en)
}
Expand Down Expand Up @@ -117,3 +121,19 @@ impl std::fmt::Display for Bytes<'_> {
}
}
}

pub trait RangeLength {
fn len(&self) -> u64;
}

impl<T> RangeLength for Range<Id<T>> {
fn len(&self) -> u64 {
self.end.value - self.start.value
}
}

impl RangeLength for Range<u64> {
fn len(&self) -> u64 {
self.end - self.start
}
}

0 comments on commit e72d11a

Please sign in to comment.