Skip to content
This repository was archived by the owner on Dec 6, 2025. It is now read-only.

Commit 2d17740

Browse files
committed
many clippy lints, FixedString inner value now private
1 parent bbe2f2f commit 2d17740

File tree

11 files changed

+59
-37
lines changed

11 files changed

+59
-37
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ missing_crate_level_docs = "warn"
5353
unwrap_used = "warn"
5454
needless_pass_by_value = "warn"
5555

56+
indexing_slicing = "warn"
57+
5658
# no_std helpers
5759
std_instead_of_core = "warn"
5860
alloc_instead_of_core = "warn"
@@ -96,7 +98,6 @@ manual_ok_or = "warn"
9698
# map_err_ignore = "warn" # broken on br(count = size) attributes
9799
map_flatten = "warn"
98100
map_unwrap_or = "warn"
99-
match_on_vec_items = "warn"
100101
match_same_arms = "warn"
101102
match_wild_err_arm = "warn"
102103
match_wildcard_for_single_variants = "warn"
@@ -119,7 +120,6 @@ single_match_else = "warn"
119120
string_add_assign = "warn"
120121
string_add = "warn"
121122
string_lit_as_bytes = "warn"
122-
string_to_string = "warn"
123123
todo = "warn"
124124
trait_duplication_in_bounds = "warn"
125125
unimplemented = "warn"

clippy.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
allow-unwrap-in-tests = true
2+
3+
# Has to be specified here, Cargo.toml can't handle these configs at workspace level. :/
4+
# https://github.com/rust-lang/cargo/issues/12917#issuecomment-1795069197
5+
doc-valid-idents = ["BWFMetaEdit", ".."]

wavrw/src/chunk/adtl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ pub struct Ltxt {
125125
/// Specifies the cue point name. This value must match one of the names listed in the `cue` chunk's [CuePoint][super::cue::CuePoint] table.
126126
pub name: u32,
127127

128-
/// Specifies the number of samples in the segment of waveform data. ...>sample_length
128+
/// Specifies the number of samples in the segment of waveform data.
129129
pub sample_length: u32,
130130

131131
/// Specifies the type or purpose of the text. For example, dwPurpose can specify a FOURCC code like `scrp` for script text or `capt` for close-caption text. `rgn ` is commonly used for "region notes"

wavrw/src/chunk/cset.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ pub struct Cset {
3131

3232
/// Specify the language and dialect used for file elements.
3333
///
34-
/// See cset_ld_map, for a list of language and dialect codes. If the CSET
34+
/// See `cset_ld_map`, for a list of language and dialect codes. If the CSET
3535
/// chunk is not present, or if these fields have value zero, assume US
3636
/// English (language code 9, dialect code 1).
3737
pub language: u16,
3838

3939
/// Specify the language and dialect used for file elements.
4040
///
41-
/// See cset_ld_map, for a list of language and dialect codes. If the CSET
41+
/// See `cset_ld_map`, for a list of language and dialect codes. If the CSET
4242
/// chunk is not present, or if these fields have value zero, assume US
4343
/// English (language code 9, dialect code 1).
4444
pub dialect: u16,

wavrw/src/chunk/fmt.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::{FourCC, KnownChunk, KnownChunkID, Summarizable};
1313

1414
/// A number indicating the WAVE format category of the file.
1515
///
16-
/// The content of the format-specific-fields [ed: everything after block_align]
16+
/// The content of the format-specific-fields [ed: everything after `block_align`]
1717
/// portion of the fmt chunk, and the interpretation of the waveform data, depend on
1818
/// this value. [RIFF1991](https://wavref.til.cafe/chunk/fmt/)
1919
#[allow(dead_code, missing_docs)]
@@ -592,7 +592,7 @@ pub trait Tag {
592592
}
593593

594594
//---------------------------
595-
/// Format of PCM audio samples in `data`. (WAVE_FORMAT_PCM) [RIFF1991](https://wavref.til.cafe/chunk/fmt/)
595+
/// Format of PCM audio samples in `data`. (`WAVE_FORMAT_PCM`) [RIFF1991](https://wavref.til.cafe/chunk/fmt/)
596596
#[binrw]
597597
#[brw(little)]
598598
#[br(import(_size: u32))]
@@ -729,7 +729,7 @@ impl Display for AdpcmCoefficients {
729729
}
730730
}
731731

732-
/// Format of ADPCM audio samples in `data`. (WAVE_FORMAT_ADPCM) [RIFF1994](https://wavref.til.cafe/chunk/fmt/)
732+
/// Format of ADPCM audio samples in `data`. (`WAVE_FORMAT_ADPCM`) [RIFF1994](https://wavref.til.cafe/chunk/fmt/)
733733
#[binrw]
734734
#[brw(little)]
735735
#[br(import(_size: u32))]
@@ -907,7 +907,7 @@ impl Iterator for FmtAdpcmIterator<'_> {
907907

908908
//---------------------------
909909

910-
/// Format of DVI ADPCM audio samples in `data`. (WAVE_FORMAT_DVI_ADPCM) [RIFF1994](https://wavref.til.cafe/chunk/fmt/)
910+
/// Format of DVI ADPCM audio samples in `data`. (`WAVE_FORMAT_DVI_ADPCM`) [RIFF1994](https://wavref.til.cafe/chunk/fmt/)
911911
#[binrw]
912912
#[brw(little)]
913913
#[br(import(_size: u32))]
@@ -939,7 +939,7 @@ pub struct FmtDviAdpcm {
939939
/// of data at a time, so the value of `block_align` can be used for
940940
/// buffer alignment.
941941
///
942-
/// |bits_per_sample | block_align |
942+
/// |`bits_per_sample` | `block_align `|
943943
/// |-|-|
944944
/// |3 | (( N * 3 ) + 1 ) * 4 * channels |
945945
/// |4 | (N + 1) * 4 * channels |
@@ -1105,8 +1105,8 @@ pub struct FmtExtended {
11051105
///
11061106
/// The size in bytes of the extra information in the WAVE format header not
11071107
/// including the size of the `FmtExtended` structure. (size of fields from
1108-
/// format_tag through extra_size inclusive (all fields except id, size and
1109-
/// the extra_bytes))
1108+
/// `format_tag` through `extra_size` inclusive (all fields except id, size and
1109+
/// the `extra_bytes`))
11101110
#[br()]
11111111
#[bw(map = |_| self.extra_bytes.len() as u16)]
11121112
pub extra_size: u16,

wavrw/src/chunk/inst.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub struct Inst {
1919
/// Pitch shift adjustment in cents.
2020
///
2121
/// Pitch shift adjustment in cents. (or 100ths of a semitone) needed to
22-
/// hit `unshifted_note` value exactly. fine_tune can be used to compensate
22+
/// hit `unshifted_note` value exactly. `fine_tune` can be used to compensate
2323
/// for tuning errors in the sampling process. Valid values range from -50
2424
/// to 50.
2525
pub fine_tune: i8,

wavrw/src/chunk/ixml/aswg.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ pub struct Aswg {
127127
/// UCS compliant SFX creator/publisher. category:Sound Effects
128128
pub creator_id: Option<String>,
129129

130-
/// UCS compliant SFX SourceID. category:Sound Effects
130+
/// UCS compliant SFX `SourceID`. category:Sound Effects
131131
pub source_id: Option<String>,
132132

133133
/// RMS power of file. category:Audio Features

wavrw/src/chunk/ixml/mod.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ impl Ixml {
183183
/// assert_eq!("My Project", ixml.project.unwrap());
184184
/// ```
185185
pub fn set(&mut self, path: &[String], value: String) {
186-
if let Some(first) = path.first() {
186+
if let Some((first, remaining_path)) = path.split_first() {
187187
match first.as_str() {
188188
"IXML_VERSION" => self.ixml_version = Some(value),
189189
"PROJECT" => self.project = Some(value),
@@ -212,7 +212,7 @@ impl Ixml {
212212
"NOTE" => self.note = Some(value),
213213
"ASWG" => {
214214
let aswg = self.aswg.get_or_insert(Aswg::new());
215-
aswg.set(&path[1..], value);
215+
aswg.set(remaining_path, value);
216216
}
217217
&_ => {
218218
self.extra.insert(path.join("/"), value);
@@ -233,7 +233,13 @@ impl Ixml {
233233
}
234234
Ok(XmlEvent::Characters(chars)) => {
235235
// explicitly strip the `BWFXML` root node before passing on.
236-
ixml.set(&path[1..], chars);
236+
if let Some(remaining_path) = &path.get(1..) {
237+
ixml.set(remaining_path, chars);
238+
} else {
239+
// TODO: actual error handling
240+
eprintln!("Error: XmlEvent::Characters, no remaining path: {path:?}");
241+
break;
242+
}
237243
}
238244
Ok(XmlEvent::EndElement { name: _ }) => {
239245
path.pop();

wavrw/src/chunk/smpl.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ pub struct Smpl {
7373
/// Period of one sample in nanoseconds.
7474
///
7575
/// Specifies the period of one sample in nanoseconds (normally 1/
76-
/// samples_per_second from the WAVEFORMAT structure for the RIFF WAVE file
76+
/// `samples_per_second` from the WAVEFORMAT structure for the RIFF WAVE file
7777
/// -- however, this field allows fine tuning). For example, 44.1 kHz would
7878
/// be specified as 22675 (0x00005893).
7979
pub sample_period: u32,
@@ -88,15 +88,15 @@ pub struct Smpl {
8888
/// Fine tune pitch as fraction of a semitone.
8989
///
9090
/// Specifies the fraction of a semitone up from the specified
91-
/// midi_unity_note. A value of 0x80000000 is 1/2 semitone (50 cents); a
91+
/// `midi_unity_note. A value of 0x80000000 is 1/2 semitone (50 cents); a
9292
/// value of 0x00000000 represents no fine tuning between semitones.
9393
pub midi_pitch_fraction: u32,
9494

9595
/// SMPTE time format.
9696
///
97-
/// Specifies the SMPTE time format used in the smpte_offset field. Possible
97+
/// Specifies the SMPTE time format used in the `smpte_offset` field. Possible
9898
/// values are (unrecognized formats should be ignored): 0 - specifies
99-
/// no SMPTE offset (smpte_offset should also be zero). 24 - specifies 24
99+
/// no SMPTE offset (`smpte_offset` should also be zero). 24 - specifies 24
100100
/// frames per second. 25 - specifies 25 frames per second. 29 - specifies
101101
/// 30 frames per second with frame dropping ('30 drop'). 30 - specifies 30
102102
/// frames per second.
@@ -108,7 +108,7 @@ pub struct Smpl {
108108
/// calibrated according to a start time other than 0. The format of this
109109
/// value is 0xhhmmssff. hh is a signed Hours value [-23..23]. mm is an
110110
/// unsigned Minutes value [0..59]. ss is unsigned Seconds value [0..59]. ff
111-
/// is an unsigned value [0..(smpte_format - 1)].
111+
/// is an unsigned value [0..(`smpte_format` - 1)].
112112
pub smpte_offset: u32,
113113

114114
/// Count of sample loops (for serialization/deserialization)

wavrw/src/fixedstring.rs

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#![doc = include_str!("fixedstring.md")]
22

33
use alloc::string::FromUtf8Error;
4-
use core::cmp::min;
54
use core::error::Error;
65
use core::fmt::{Debug, Display, Formatter};
76
use core::str::FromStr;
@@ -65,7 +64,9 @@ impl From<FromUtf8Error> for FixedStringError {
6564

6665
#[doc = include_str!("fixedstring.md")]
6766
#[derive(Clone, PartialEq, Eq, Hash)]
68-
pub struct FixedString<const N: usize>(String);
67+
pub struct FixedString<const N: usize> {
68+
inner: String,
69+
}
6970
// This is only immutable because it would be a lot of work to correctly DeRef
7071
// to the inner string while still enforcing the length constraint. Design
7172
// quesion: is it worth the work? Maybe if it turns out to be annoying to work
@@ -81,7 +82,7 @@ impl<const N: usize> Debug for FixedString<N> {
8182

8283
impl<const N: usize> Display for FixedString<N> {
8384
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), core::fmt::Error> {
84-
write!(f, "{}", &self.0)
85+
write!(f, "{}", &self.inner)
8586
}
8687
}
8788

@@ -140,7 +141,7 @@ impl<const N: usize> FixedString<N> {
140141
}
141142
let s = alloc::string::String::from_utf8(vec)?;
142143
let s = s.trim_end_matches('\0').to_string();
143-
Ok(Self(s))
144+
Ok(Self { inner: s })
144145
}
145146

146147
/// Create a new [u8; N] from &self
@@ -163,16 +164,20 @@ impl<const N: usize> FixedString<N> {
163164
/// ```
164165
pub fn to_bytes(&self) -> [u8; N] {
165166
let mut array_tmp = [0u8; N];
166-
let bytes = self.0.as_bytes();
167-
let l = min(bytes.len(), N);
168-
array_tmp[..l].copy_from_slice(&bytes[..l]);
167+
let bytes = self.inner.as_bytes();
168+
// copy_from_slice requires slices of same length or it panics
169+
// self.inner.len() <= N bytes because it's also defined by N (enforced by
170+
// constructors and From impls - inner is not public.
171+
array_tmp[..bytes.len()].copy_from_slice(bytes);
169172
array_tmp
170173
}
171174
}
172175

173176
impl<const N: usize> Default for FixedString<N> {
174177
fn default() -> Self {
175-
FixedString::<N>(String::new())
178+
FixedString::<N> {
179+
inner: String::new(),
180+
}
176181
}
177182
}
178183

@@ -186,7 +191,9 @@ impl<const N: usize> FromStr for FixedString<N> {
186191
len: s.len(),
187192
});
188193
}
189-
Ok(FixedString(s.to_string()))
194+
Ok(FixedString {
195+
inner: s.to_string(),
196+
})
190197
}
191198
}
192199

@@ -201,6 +208,7 @@ impl<const N: usize> BinRead for FixedString<N> {
201208
let mut values: [u8; N] = [0; N];
202209
let mut index = 0;
203210

211+
#[allow(clippy::indexing_slicing)]
204212
loop {
205213
if index >= N {
206214
return match Self::from_utf8(values.to_vec()) {
@@ -231,6 +239,9 @@ impl<const N: usize> BinRead for FixedString<N> {
231239
}),
232240
};
233241
}
242+
// #[allow(clippy::indexing_slicing)] set above because attributes on
243+
// expressions are experimental (at the time of writing).
244+
// index >= N checked at top of loop
234245
values[index] = val;
235246
index += 1;
236247
}
@@ -246,7 +257,7 @@ impl<const N: usize> BinWrite for FixedString<N> {
246257
endian: Endian,
247258
args: Self::Args<'_>,
248259
) -> BinResult<()> {
249-
let bytes = self.0.as_bytes();
260+
let bytes = self.inner.as_bytes();
250261
let padding_size = N - bytes.len();
251262
bytes.write_options(writer, endian, args)?;
252263
for _ in 0..padding_size {
@@ -266,7 +277,7 @@ mod test {
266277

267278
#[test]
268279
fn fixed_string() {
269-
let fs = FixedString::<6>("abc".to_string());
280+
let fs = FixedString::<6>::from_str("abc").unwrap();
270281
assert_eq!(6, fs.len());
271282
let s = fs.to_string();
272283
assert_eq!("abc".to_string(), s);
@@ -289,10 +300,10 @@ mod test {
289300
fn fixed_string_long() {
290301
// strings longer than fixed size should get truncated
291302

292-
// initializing with ::new() truncates without error
293-
// let long_str = "this is a longer str";
294-
// let fs = FixedString::<6>::new(long_str);
295-
// assert_eq!(fs.to_string(), "this i");
303+
// initializing with ::from_utf8() truncates without error
304+
let long_str = "this is a longer str";
305+
let err = FixedString::<6>::from_utf8(long_str.to_string().into());
306+
assert_eq!(err, Err(FixedStringError::Truncated { limit: 6, len: 20 }));
296307

297308
// via FromStr returns an error
298309
let long_str = "this is a longer str";

0 commit comments

Comments
 (0)