Skip to content

Commit 8678243

Browse files
committed
io: Impl Length for mutable refs
1 parent e0bea0c commit 8678243

File tree

4 files changed

+74
-3
lines changed

4 files changed

+74
-3
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
### Added
10+
- **Length**: `impl<T: Length> Truncate for &mut T` ([PR](https://github.com/Serial-ATA/lofty-rs/pull/387))
11+
912
## [0.19.1] - 2024-04-26
1013

1114
### Added
@@ -726,7 +729,8 @@ See [ogg_pager's changelog](ogg_pager/CHANGELOG.md).
726729
### Removed
727730
- `ErrorKind::BadExtension`
728731

729-
[Unreleased]: https://github.com/Serial-ATA/lofty-rs/compare/0.19.1...HEAD
732+
[Unreleased]: https://github.com/Serial-ATA/lofty-rs/compare/0.19.2...HEAD
733+
[0.19.2]: https://github.com/Serial-ATA/lofty-rs/compare/0.19.1...0.19.2
730734
[0.19.1]: https://github.com/Serial-ATA/lofty-rs/compare/0.19.0...0.19.1
731735
[0.19.0]: https://github.com/Serial-ATA/lofty-rs/compare/0.18.2...0.19.0
732736
[0.18.2]: https://github.com/Serial-ATA/lofty-rs/compare/0.18.1...0.18.2

lofty/src/iff/wav/tag/write.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ pub(super) fn create_riff_info(
124124
bytes.extend(terminator);
125125
}
126126

127-
let packet_size = bytes.len() - 4;
127+
let packet_size = Vec::len(bytes) - 4;
128128

129129
if packet_size > u32::MAX as usize {
130130
err!(TooMuchData);

lofty/src/mp4/ilst/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ impl SplitTag for Ilst {
599599
ItemValue::Text(text)
600600
},
601601
// We have to special case track/disc numbers since they are stored together
602-
AtomData::Unknown { code: 0, data } if data.len() >= 6 => {
602+
AtomData::Unknown { code: 0, data } if Vec::len(data) >= 6 => {
603603
if let AtomIdent::Fourcc(ref fourcc) = ident {
604604
match fourcc {
605605
b"trkn" => {

lofty/src/util/io.rs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,17 @@ impl<T> SeekStreamLen for T where T: Seek {}
2929
///
3030
/// Take great care in implementing this for downstream types, as Lofty will assume that the
3131
/// container has the new length specified. If this assumption were to be broken, files **will** become corrupted.
32+
///
33+
/// # Examples
34+
///
35+
/// ```rust
36+
/// use lofty::io::Truncate;
37+
///
38+
/// let mut data = vec![1, 2, 3, 4, 5];
39+
/// data.truncate(3);
40+
///
41+
/// assert_eq!(data, vec![1, 2, 3]);
42+
/// ```
3243
pub trait Truncate {
3344
/// The error type of the truncation operation
3445
type Error: Into<LoftyError>;
@@ -107,6 +118,15 @@ where
107118
///
108119
/// Take great care in implementing this for downstream types, as Lofty will assume that the
109120
/// container has the exact length specified. If this assumption were to be broken, files **may** become corrupted.
121+
///
122+
/// # Examples
123+
///
124+
/// ```rust
125+
/// use lofty::io::Length;
126+
///
127+
/// let data = vec![1, 2, 3, 4, 5];
128+
/// assert_eq!(data.len(), 5);
129+
/// ```
110130
pub trait Length {
111131
/// The error type of the length operation
112132
type Error: Into<LoftyError>;
@@ -176,6 +196,17 @@ where
176196
}
177197
}
178198

199+
impl<T> Length for &mut T
200+
where
201+
T: Length,
202+
{
203+
type Error = <T as Length>::Error;
204+
205+
fn len(&self) -> Result<u64, Self::Error> {
206+
Length::len(*self)
207+
}
208+
}
209+
179210
/// Provides a set of methods to read and write to a file-like object
180211
///
181212
/// This is a combination of the [`Read`], [`Write`], [`Seek`], [`Truncate`], and [`Length`] traits.
@@ -284,4 +315,40 @@ mod tests {
284315
let current_file_contents = reader.into_inner();
285316
assert_eq!(current_file_contents, test_asset_contents());
286317
}
318+
319+
#[test]
320+
fn io_save_using_references() {
321+
struct File {
322+
buf: Vec<u8>,
323+
}
324+
325+
let mut f = File {
326+
buf: std::fs::read(TEST_ASSET).unwrap(),
327+
};
328+
329+
// Same test as above, but using references instead of owned values
330+
let mut file = file();
331+
alter_tag(&mut file);
332+
333+
{
334+
let mut reader = Cursor::new(&mut f.buf);
335+
file.save_to(&mut reader, WriteOptions::new().preferred_padding(0))
336+
.expect("Failed to save to vec");
337+
}
338+
339+
{
340+
let mut reader = Cursor::new(&f.buf[..]);
341+
file = MpegFile::read_from(&mut reader, ParseOptions::new()).unwrap();
342+
revert_tag(&mut file);
343+
}
344+
345+
{
346+
let mut reader = Cursor::new(&mut f.buf);
347+
file.save_to(&mut reader, WriteOptions::new().preferred_padding(0))
348+
.expect("Failed to save to vec");
349+
}
350+
351+
let current_file_contents = f.buf;
352+
assert_eq!(current_file_contents, test_asset_contents());
353+
}
287354
}

0 commit comments

Comments
 (0)