@@ -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+ /// ```
3243pub 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+ /// ```
110130pub 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