|
| 1 | +//! Various traits for reading and writing to file-like objects |
| 2 | +
|
| 3 | +use crate::error::{AudioError, Result}; |
| 4 | +use crate::math::F80; |
| 5 | + |
| 6 | +use std::collections::VecDeque; |
| 7 | +use std::fs::File; |
| 8 | +use std::io::{Cursor, Read, Seek, Write}; |
| 9 | + |
| 10 | +/// Provides a method to truncate an object to the specified length |
| 11 | +/// |
| 12 | +/// This is one component of the [`FileLike`] trait, which is used to provide implementors access to any |
| 13 | +/// file saving methods such as [`AudioFile::save_to`](crate::file::AudioFile::save_to). |
| 14 | +/// |
| 15 | +/// Take great care in implementing this for downstream types, as Lofty will assume that the |
| 16 | +/// container has the new length specified. If this assumption were to be broken, files **will** become corrupted. |
| 17 | +/// |
| 18 | +/// # Examples |
| 19 | +/// |
| 20 | +/// ```rust |
| 21 | +/// use lofty::io::Truncate; |
| 22 | +/// |
| 23 | +/// let mut data = vec![1, 2, 3, 4, 5]; |
| 24 | +/// data.truncate(3); |
| 25 | +/// |
| 26 | +/// assert_eq!(data, vec![1, 2, 3]); |
| 27 | +/// ``` |
| 28 | +pub trait Truncate { |
| 29 | + /// The error type of the truncation operation |
| 30 | + type Error: Into<AudioError>; |
| 31 | + |
| 32 | + /// Truncate a storage object to the specified length |
| 33 | + /// |
| 34 | + /// # Errors |
| 35 | + /// |
| 36 | + /// Errors depend on the object being truncated, which may not always be fallible. |
| 37 | + fn truncate(&mut self, new_len: u64) -> std::result::Result<(), Self::Error>; |
| 38 | +} |
| 39 | + |
| 40 | +impl Truncate for File { |
| 41 | + type Error = std::io::Error; |
| 42 | + |
| 43 | + fn truncate(&mut self, new_len: u64) -> std::result::Result<(), Self::Error> { |
| 44 | + self.set_len(new_len) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +impl Truncate for Vec<u8> { |
| 49 | + type Error = std::convert::Infallible; |
| 50 | + |
| 51 | + fn truncate(&mut self, new_len: u64) -> std::result::Result<(), Self::Error> { |
| 52 | + self.truncate(new_len as usize); |
| 53 | + Ok(()) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +impl Truncate for VecDeque<u8> { |
| 58 | + type Error = std::convert::Infallible; |
| 59 | + |
| 60 | + fn truncate(&mut self, new_len: u64) -> std::result::Result<(), Self::Error> { |
| 61 | + self.truncate(new_len as usize); |
| 62 | + Ok(()) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +impl<T> Truncate for Cursor<T> |
| 67 | +where |
| 68 | + T: Truncate, |
| 69 | +{ |
| 70 | + type Error = <T as Truncate>::Error; |
| 71 | + |
| 72 | + fn truncate(&mut self, new_len: u64) -> std::result::Result<(), Self::Error> { |
| 73 | + self.get_mut().truncate(new_len) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +impl<T> Truncate for Box<T> |
| 78 | +where |
| 79 | + T: Truncate, |
| 80 | +{ |
| 81 | + type Error = <T as Truncate>::Error; |
| 82 | + |
| 83 | + fn truncate(&mut self, new_len: u64) -> std::result::Result<(), Self::Error> { |
| 84 | + self.as_mut().truncate(new_len) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +impl<T> Truncate for &mut T |
| 89 | +where |
| 90 | + T: Truncate, |
| 91 | +{ |
| 92 | + type Error = <T as Truncate>::Error; |
| 93 | + |
| 94 | + fn truncate(&mut self, new_len: u64) -> std::result::Result<(), Self::Error> { |
| 95 | + (**self).truncate(new_len) |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +/// Provides a method to get the length of a storage object |
| 100 | +/// |
| 101 | +/// This is one component of the [`FileLike`] trait, which is used to provide implementors access to any |
| 102 | +/// file saving methods such as [`AudioFile::save_to`](crate::file::AudioFile::save_to). |
| 103 | +/// |
| 104 | +/// Take great care in implementing this for downstream types, as Lofty will assume that the |
| 105 | +/// container has the exact length specified. If this assumption were to be broken, files **may** become corrupted. |
| 106 | +/// |
| 107 | +/// # Examples |
| 108 | +/// |
| 109 | +/// ```rust |
| 110 | +/// use lofty::io::Length; |
| 111 | +/// |
| 112 | +/// let data = vec![1, 2, 3, 4, 5]; |
| 113 | +/// assert_eq!(data.len(), 5); |
| 114 | +/// ``` |
| 115 | +pub trait Length { |
| 116 | + /// The error type of the length operation |
| 117 | + type Error: Into<AudioError>; |
| 118 | + |
| 119 | + /// Get the length of a storage object |
| 120 | + /// |
| 121 | + /// # Errors |
| 122 | + /// |
| 123 | + /// Errors depend on the object being read, which may not always be fallible. |
| 124 | + fn len(&self) -> std::result::Result<u64, Self::Error>; |
| 125 | +} |
| 126 | + |
| 127 | +impl Length for File { |
| 128 | + type Error = std::io::Error; |
| 129 | + |
| 130 | + fn len(&self) -> std::result::Result<u64, Self::Error> { |
| 131 | + self.metadata().map(|m| m.len()) |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +impl Length for Vec<u8> { |
| 136 | + type Error = std::convert::Infallible; |
| 137 | + |
| 138 | + fn len(&self) -> std::result::Result<u64, Self::Error> { |
| 139 | + Ok(self.len() as u64) |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +impl Length for VecDeque<u8> { |
| 144 | + type Error = std::convert::Infallible; |
| 145 | + |
| 146 | + fn len(&self) -> std::result::Result<u64, Self::Error> { |
| 147 | + Ok(self.len() as u64) |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +impl<T> Length for Cursor<T> |
| 152 | +where |
| 153 | + T: Length, |
| 154 | +{ |
| 155 | + type Error = <T as Length>::Error; |
| 156 | + |
| 157 | + fn len(&self) -> std::result::Result<u64, Self::Error> { |
| 158 | + Length::len(self.get_ref()) |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +impl<T> Length for Box<T> |
| 163 | +where |
| 164 | + T: Length, |
| 165 | +{ |
| 166 | + type Error = <T as Length>::Error; |
| 167 | + |
| 168 | + fn len(&self) -> std::result::Result<u64, Self::Error> { |
| 169 | + Length::len(self.as_ref()) |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +impl<T> Length for &T |
| 174 | +where |
| 175 | + T: Length, |
| 176 | +{ |
| 177 | + type Error = <T as Length>::Error; |
| 178 | + |
| 179 | + fn len(&self) -> std::result::Result<u64, Self::Error> { |
| 180 | + Length::len(*self) |
| 181 | + } |
| 182 | +} |
| 183 | + |
| 184 | +impl<T> Length for &mut T |
| 185 | +where |
| 186 | + T: Length, |
| 187 | +{ |
| 188 | + type Error = <T as Length>::Error; |
| 189 | + |
| 190 | + fn len(&self) -> std::result::Result<u64, Self::Error> { |
| 191 | + Length::len(*self) |
| 192 | + } |
| 193 | +} |
| 194 | + |
| 195 | +/// Provides a set of methods to read and write to a file-like object |
| 196 | +/// |
| 197 | +/// This is a combination of the [`Read`], [`Write`], [`Seek`], [`Truncate`], and [`Length`] traits. |
| 198 | +/// It is used to provide implementors access to any file saving methods such as [`AudioFile::save_to`](crate::file::AudioFile::save_to). |
| 199 | +/// |
| 200 | +/// Take great care in implementing this for downstream types, as Lofty will assume that the |
| 201 | +/// trait implementations are correct. If this assumption were to be broken, files **may** become corrupted. |
| 202 | +pub trait FileLike: Read + Write + Seek + Truncate + Length |
| 203 | +where |
| 204 | + <Self as Truncate>::Error: Into<AudioError>, |
| 205 | + <Self as Length>::Error: Into<AudioError>, |
| 206 | +{ |
| 207 | +} |
| 208 | + |
| 209 | +impl<T> FileLike for T |
| 210 | +where |
| 211 | + T: Read + Write + Seek + Truncate + Length, |
| 212 | + <T as Truncate>::Error: Into<AudioError>, |
| 213 | + <T as Length>::Error: Into<AudioError>, |
| 214 | +{ |
| 215 | +} |
| 216 | + |
| 217 | +pub trait ReadExt: Read { |
| 218 | + fn read_f80(&mut self) -> Result<F80>; |
| 219 | +} |
| 220 | + |
| 221 | +impl<R> ReadExt for R |
| 222 | +where |
| 223 | + R: Read, |
| 224 | +{ |
| 225 | + fn read_f80(&mut self) -> Result<F80> { |
| 226 | + let mut bytes = [0; 10]; |
| 227 | + self.read_exact(&mut bytes)?; |
| 228 | + |
| 229 | + Ok(F80::from_be_bytes(bytes)) |
| 230 | + } |
| 231 | +} |
0 commit comments