Skip to content

Commit

Permalink
fix some clippy warnings (automatic)
Browse files Browse the repository at this point in the history
  • Loading branch information
ekiwi committed May 7, 2024
1 parent 873a7b3 commit b305227
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 105 deletions.
2 changes: 1 addition & 1 deletion src/fst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ pub(crate) fn parse_var_attributes(
type_name = Some(name);
// For now we ignore the var type since GHDL seems to just always set it to Signal.
// Their code does not use any other var type.
deal_with_vhdl_var_type(vhdl_var_type, &var_name);
deal_with_vhdl_var_type(vhdl_var_type, var_name);

// We merge the info of the VCD var type and the vhdl data type
var_type = merge_vhdl_data_and_var_type(var_type, vhdl_data_type);
Expand Down
6 changes: 3 additions & 3 deletions src/ghw/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ pub(crate) fn check_header_zeros(section: &'static str, header: &[u8]) -> Result
section,
format!(
"first four bytes should be zero and not {}",
String::from_utf8_lossy(&zeros)
String::from_utf8_lossy(zeros)
),
))
}
Expand Down Expand Up @@ -224,8 +224,8 @@ impl GhwSignalInfo {
pub fn tpe(&self) -> SignalType {
let value = self.tpe_and_vec.get();
let raw_tpe = (value & 0x7) as u8;
let tpe = SignalType::try_from_primitive(raw_tpe - 1).unwrap();
tpe

SignalType::try_from_primitive(raw_tpe - 1).unwrap()
}
}

Expand Down
24 changes: 12 additions & 12 deletions src/ghw/hierarchy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ fn add_enums_to_wellen_hierarchy(
let mut out = Vec::new();
for tpe in tables.types.iter() {
if let VhdlType::Enum(name, lits, enum_id) = tpe {
let bits = get_enum_bits(&lits) as u16;
let bits = get_enum_bits(lits) as u16;
let literals: Vec<_> = lits
.iter()
.enumerate()
Expand Down Expand Up @@ -239,8 +239,8 @@ fn add_enums_to_wellen_hierarchy(

fn get_enum_bits(literals: &[StringId]) -> u32 {
let max_value = literals.len() as u64 - 1;
let bits = u64::BITS - max_value.leading_zeros();
bits

u64::BITS - max_value.leading_zeros()
}

fn read_string_section(header: &HeaderData, input: &mut impl BufRead) -> Result<Vec<String>> {
Expand All @@ -260,7 +260,7 @@ fn read_string_section(header: &HeaderData, input: &mut impl BufRead) -> Result<
let mut c;
loop {
c = read_u8(input)?;
if c <= 31 || (c >= 128 && c <= 159) {
if c <= 31 || (128..=159).contains(&c) {
break;
} else {
buf.push(c);
Expand Down Expand Up @@ -506,7 +506,7 @@ impl VhdlType {
(VhdlType::Array(base_name, element_tpe, maybe_base_range), Range::Int(int_range)) => {
if let Some(base_range) = maybe_base_range {
debug_assert!(
int_range.is_subset_of(&base_range),
int_range.is_subset_of(base_range),
"{int_range:?} {base_range:?}"
);
}
Expand All @@ -518,14 +518,14 @@ impl VhdlType {
}
(VhdlType::NineValueVec(base_name, base_range), Range::Int(int_range)) => {
debug_assert!(
int_range.is_subset_of(&base_range),
int_range.is_subset_of(base_range),
"{int_range:?} {base_range:?}"
);
VhdlType::NineValueVec(pick_best_name(name, *base_name), int_range)
}
(VhdlType::BitVec(base_name, base_range), Range::Int(int_range)) => {
debug_assert!(
int_range.is_subset_of(&base_range),
int_range.is_subset_of(base_range),
"{int_range:?} {base_range:?}"
);
VhdlType::BitVec(pick_best_name(name, *base_name), int_range)
Expand Down Expand Up @@ -970,7 +970,7 @@ impl GhwSignalTracker {
let sliced_signal = self.vectors[vec_id.index()].signal_ref();
if let Some(mut alias_id) = self.vectors[vec_id.index()].alias() {
loop {
let alias = self.aliases[alias_id.get() as usize - 1].clone();
let alias = self.aliases[alias_id.get() as usize - 1];
if alias.msb == msb && alias.lsb == lsb {
return alias.signal_ref;
}
Expand Down Expand Up @@ -1113,7 +1113,7 @@ fn add_var(
VhdlType::Enum(_, literals, enum_id) => {
let enum_type = tables.enums[*enum_id as usize];
let index = read_signal_id(input, signals.max_signal_id())?;
let bits = get_enum_bits(&literals);
let bits = get_enum_bits(literals);
let signal_ref = signals.register_scalar(index, SignalType::U8);
h.add_var(
name,
Expand Down Expand Up @@ -1184,7 +1184,7 @@ fn add_var(
);
}
VhdlType::NineValueVec(_, range) | VhdlType::BitVec(_, range) => {
let num_bits = range.len().abs() as u32;
let num_bits = range.len().unsigned_abs() as u32;
if num_bits == 0 {
// TODO: how should we correctly deal with an empty vector?
return Ok(());
Expand All @@ -1209,8 +1209,8 @@ fn add_var(
}

let is_binary = matches!(vhdl_tpe, VhdlType::BitVec(_, _));
let min = signal_ids.first().unwrap().clone();
let max = signal_ids.last().unwrap().clone();
let min = *signal_ids.first().unwrap();
let max = *signal_ids.last().unwrap();
let signal_ref = signals.register_bit_vec(min, max, is_binary);
let var_type = match h.get_str(tpe_name).to_ascii_lowercase().as_str() {
"std_ulogic_vector" => VarType::StdULogicVector,
Expand Down
2 changes: 1 addition & 1 deletion src/ghw/signals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ impl VecBuffer {
// clear signal change
let byte = vector_id.index() / 8;
let bit = vector_id.index() % 8;
self.signal_change[byte] = self.signal_change[byte] & !(1u8 << bit);
self.signal_change[byte] &= !(1u8 << bit);
// note, we keep the signal on the change list

// return reference to value
Expand Down
112 changes: 46 additions & 66 deletions src/hierarchy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,7 @@ pub struct VarRef(NonZeroU32);
impl VarRef {
#[inline]
fn from_index(index: usize) -> Option<Self> {
match NonZeroU32::new(index as u32 + 1) {
None => None,
Some(value) => Some(VarRef(value)),
}
NonZeroU32::new(index as u32 + 1).map(VarRef)
}

#[inline]
Expand All @@ -83,10 +80,7 @@ pub struct ScopeRef(NonZeroU16);
impl ScopeRef {
#[inline]
fn from_index(index: usize) -> Option<Self> {
match NonZeroU16::new(index as u16 + 1) {
None => None,
Some(value) => Some(Self(value)),
}
NonZeroU16::new(index as u16 + 1).map(Self)
}

#[inline]
Expand Down Expand Up @@ -242,10 +236,7 @@ pub struct SignalRef(NonZeroU32);
impl SignalRef {
#[inline]
pub fn from_index(index: usize) -> Option<Self> {
match NonZeroU32::new(index as u32 + 1) {
None => None,
Some(value) => Some(Self(value)),
}
NonZeroU32::new(index as u32 + 1).map(Self)
}

#[inline]
Expand Down Expand Up @@ -655,20 +646,20 @@ impl Hierarchy {

/// Returns an iterator over all top-level scopes and variables.
pub fn items(&self) -> HierarchyItemIterator {
HierarchyItemIterator::new(&self, self.first_item)
HierarchyItemIterator::new(self, self.first_item)
}

/// Returns an iterator over references to all top-level scopes.
pub fn scopes(&self) -> HierarchyScopeRefIterator {
HierarchyScopeRefIterator {
underlying: HierarchyItemIdIterator::new(&self, self.first_item),
underlying: HierarchyItemIdIterator::new(self, self.first_item),
}
}

/// Returns an iterator over references to all top-level variables.
pub fn vars(&self) -> HierarchyVarRefIterator {
HierarchyVarRefIterator {
underlying: HierarchyItemIdIterator::new(&self, self.first_item),
underlying: HierarchyItemIdIterator::new(self, self.first_item),
}
}

Expand Down Expand Up @@ -720,12 +711,12 @@ impl Hierarchy {

pub fn lookup_scope<N: AsRef<str>>(&self, names: &[N]) -> Option<ScopeRef> {
let prefix = names.first()?.as_ref();
let mut scope = self.scopes().find(|s| self.get(*s).name(&self) == prefix)?;
let mut scope = self.scopes().find(|s| self.get(*s).name(self) == prefix)?;
for name in names.iter().skip(1) {
scope = self
.get(scope)
.scopes(&self)
.find(|s| self.get(*s).name(&self) == name.as_ref())?;
.scopes(self)
.find(|s| self.get(*s).name(self) == name.as_ref())?;
}
Some(scope)
}
Expand All @@ -734,12 +725,12 @@ impl Hierarchy {
match path {
[] => self
.vars()
.find(|v| self.get(*v).name(&self) == name.as_ref()),
.find(|v| self.get(*v).name(self) == name.as_ref()),
scopes => {
let scope = self.get(self.lookup_scope(scopes)?);
scope
.vars(&self)
.find(|v| self.get(*v).name(&self) == name.as_ref())
.vars(self)
.find(|v| self.get(*v).name(self) == name.as_ref())
}
}
}
Expand Down Expand Up @@ -1011,53 +1002,42 @@ impl HierarchyBuilder {
last_child,
flattened: false,
})
} else if flatten {
self.scope_stack.push(ScopeStackEntry {
scope_id: usize::MAX,
last_child: None,
flattened: true,
});
} else {
if flatten {
self.scope_stack.push(ScopeStackEntry {
scope_id: usize::MAX,
last_child: None,
flattened: true,
});
} else {
let node_id = self.scopes.len();
let wrapped_id = HierarchyItemId::Scope(ScopeRef::from_index(node_id).unwrap());
if self.first_item.is_none() {
self.first_item = Some(wrapped_id);
}
let parent = self.add_to_hierarchy_tree(wrapped_id);

// new active scope
self.scope_stack.push(ScopeStackEntry {
scope_id: node_id,
last_child: None,
flattened: false,
});

// empty component name is treated the same as none
let component = match component {
None => None,
Some(name) => {
if name == EMPTY_STRING {
None
} else {
Some(name)
}
}
};

// now we can build the node data structure and store it
let node = Scope {
parent,
child: None,
next: None,
name,
component,
tpe,
declaration_source,
instance_source,
};
self.scopes.push(node);
let node_id = self.scopes.len();
let wrapped_id = HierarchyItemId::Scope(ScopeRef::from_index(node_id).unwrap());
if self.first_item.is_none() {
self.first_item = Some(wrapped_id);
}
let parent = self.add_to_hierarchy_tree(wrapped_id);

// new active scope
self.scope_stack.push(ScopeStackEntry {
scope_id: node_id,
last_child: None,
flattened: false,
});

// empty component name is treated the same as none
let component = component.filter(|&name| name != EMPTY_STRING);

// now we can build the node data structure and store it
let node = Scope {
parent,
child: None,
next: None,
name,
component,
tpe,
declaration_source,
instance_source,
};
self.scopes.push(node);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub mod viewers;
mod wavemem;

/// Cargo.toml version of this library.
pub const VERSION: &'static str = env!("CARGO_PKG_VERSION");
pub const VERSION: &str = env!("CARGO_PKG_VERSION");

#[derive(Debug, PartialEq, Copy, Clone)]
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
Expand Down
2 changes: 1 addition & 1 deletion src/signals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ impl Signal {
}

pub fn iter_changes(&self) -> SignalChangeIterator {
SignalChangeIterator::new(&self)
SignalChangeIterator::new(self)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub fn read(filename: &str) -> Result<Waveform> {

/// Read a waveform file. Reads in header and body at once.
pub fn read_with_options(filename: &str, options: &LoadOptions) -> Result<Waveform> {
let header = viewers::read_header(&filename, &options)?;
let header = viewers::read_header(filename, options)?;
let body = viewers::read_body(header.body, &header.hierarchy, None)?;
Ok(Waveform::new(
header.hierarchy,
Expand Down
8 changes: 4 additions & 4 deletions src/vcd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub(crate) fn read_header(
let input_file = std::fs::File::open(filename)?;
let mmap = unsafe { memmap2::Mmap::map(&input_file)? };
let (header_len, hierarchy, lookup) =
read_hierarchy(&mut std::io::Cursor::new(&mmap[..]), &options)?;
read_hierarchy(&mut std::io::Cursor::new(&mmap[..]), options)?;
let body_len = (mmap.len() - header_len) as u64;
let cont = ReadBodyContinuation {
multi_thread: options.multi_thread,
Expand All @@ -72,7 +72,7 @@ pub(crate) fn read_header_from_bytes(
options: &LoadOptions,
) -> Result<(Hierarchy, ReadBodyContinuation, u64)> {
let (header_len, hierarchy, lookup) =
read_hierarchy(&mut std::io::Cursor::new(&bytes), &options)?;
read_hierarchy(&mut std::io::Cursor::new(&bytes), options)?;
let body_len = (bytes.len() - header_len) as u64;
let cont = ReadBodyContinuation {
multi_thread: options.multi_thread,
Expand Down Expand Up @@ -720,7 +720,7 @@ fn read_command<'a>(input: &mut impl BufRead, buf: &'a mut Vec<u8>) -> Result<(V
read_token(input, buf)?;

// check to see if this is a valid command
let cmd = VcdCmd::from_bytes_or_panic(&buf);
let cmd = VcdCmd::from_bytes_or_panic(buf);
buf.clear();

// read until we find the end token
Expand Down Expand Up @@ -1261,7 +1261,7 @@ x%i"
"%i\" = x",
"j2! = 0",
];
let res = read_body_to_vec(&mut input.as_bytes());
let res = read_body_to_vec(input.as_bytes());
assert_eq!(res, expected);
}

Expand Down
Loading

0 comments on commit b305227

Please sign in to comment.