| 1 | use craterust-analyzer cargo ogg_pager 0.7.2 crate/::Pagerust-analyzer cargo ogg_pager 0.7.2 Page#; |
| 2 | use craterust-analyzer cargo ogg_pager 0.7.2 crate/::errorrust-analyzer cargo ogg_pager 0.7.2 error/::{PageErrorrust-analyzer cargo ogg_pager 0.7.2 error/PageError#, Resultrust-analyzer cargo ogg_pager 0.7.2 error/Result#}; |
| 3 | use craterust-analyzer cargo ogg_pager 0.7.2 crate/::headerrust-analyzer cargo ogg_pager 0.7.2 header/::PageHeaderrust-analyzer cargo ogg_pager 0.7.2 header/PageHeader#; |
| 4 | use craterust-analyzer cargo ogg_pager 0.7.2 crate/::paginaterust-analyzer cargo ogg_pager 0.7.2 paginate/::paginaterust-analyzer cargo ogg_pager 0.7.2 paginate/paginate().; |
| 5 | |
| 6 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::{Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug#, Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#}; |
| 7 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::{Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#, Seekrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Seek#, Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#}; |
| 8 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::opsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/::Rangerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#; |
| 9 | |
| 10 | /// A container for packets in an OGG file |
| 11 | pub struct Packetsrust-analyzer cargo ogg_pager 0.7.2 packets/Packets# { |
| 12 | contentrust-analyzer cargo ogg_pager 0.7.2 packets/Packets#content.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>, |
| 13 | packet_sizesrust-analyzer cargo ogg_pager 0.7.2 packets/Packets#packet_sizes.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u64>, |
| 14 | } |
| 15 | |
| 16 | impl Packetsrust-analyzer cargo ogg_pager 0.7.2 packets/Packets# { |
| 17 | /// Read as many packets as possible from a reader |
| 18 | /// |
| 19 | /// # Errors |
| 20 | /// |
| 21 | /// A page has a bad length |
| 22 | /// |
| 23 | /// # Examples |
| 24 | /// |
| 25 | /// ```rust |
| 26 | /// use ogg_pager::Packets; |
| 27 | /// |
| 28 | /// # fn main() -> Result<(), ogg_pager::PageError> { |
| 29 | /// # let path = "../lofty/tests/files/assets/minimal/full_test.ogg"; |
| 30 | /// let mut file = std::fs::File::open(path)?; |
| 31 | /// |
| 32 | /// let packets = Packets::read(&mut file)?; |
| 33 | /// # Ok(()) } |
| 34 | /// ``` |
| 35 | pub fn readrust-analyzer cargo ogg_pager 0.7.2 packets/impl#[Packets]read().<R>(data: &mut R) -> Resultrust-analyzer cargo ogg_pager 0.7.2 error/Result#<Selfrust-analyzer cargo ogg_pager 0.7.2 packets/impl#[Packets]> |
| 36 | where |
| 37 | R: Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read# + Seekrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Seek#, |
| 38 | { |
| 39 | Selfrust-analyzer cargo ogg_pager 0.7.2 packets/impl#[Packets]::read_countrust-analyzer cargo ogg_pager 0.7.2 packets/impl#[Packets]read_count().(data, -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[isize][Neg]neg().1) |
| 40 | } |
| 41 | |
| 42 | /// Read a specific number of packets from a reader |
| 43 | /// |
| 44 | /// A special value of `-1` will read as many packets as possible, |
| 45 | /// in which case [`Packets::read`] should be used. |
| 46 | /// |
| 47 | /// NOTE: Any value 0 or below will return an empty [`Packets`] |
| 48 | /// |
| 49 | /// # Errors |
| 50 | /// |
| 51 | /// * Unable to read the specified number of packets |
| 52 | /// * A page has a bad length |
| 53 | /// |
| 54 | /// # Examples |
| 55 | /// |
| 56 | /// ```rust |
| 57 | /// use ogg_pager::Packets; |
| 58 | /// |
| 59 | /// # fn main() -> Result<(), ogg_pager::PageError> { |
| 60 | /// # let path = "../lofty/tests/files/assets/minimal/full_test.ogg"; |
| 61 | /// let mut file = std::fs::File::open(path)?; |
| 62 | /// |
| 63 | /// // We know that the file has at least 2 packets in it |
| 64 | /// let packets = Packets::read_count(&mut file, 2)?; |
| 65 | /// # Ok(()) } |
| 66 | /// ``` |
| 67 | #[allow(clippy::read_zero_byte_vec)] |
| 68 | pub fn read_countrust-analyzer cargo ogg_pager 0.7.2 packets/impl#[Packets]read_count().<R>(data: &mut R, count: isize) -> Resultrust-analyzer cargo ogg_pager 0.7.2 error/Result#<Selfrust-analyzer cargo ogg_pager 0.7.2 packets/impl#[Packets]> |
| 69 | where |
| 70 | R: Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read# + Seekrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Seek#, |
| 71 | { |
| 72 | let mut content = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().(); |
| 73 | let mut packet_sizes = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().(); |
| 74 | |
| 75 | if count rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[isize][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[isize][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[isize][`PartialEq<Self>`]eq().0 || count rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[isize][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[isize][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[isize][`PartialOrd<Self>`]lt().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[isize][Neg]neg().1 { |
| 76 | return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Selfrust-analyzer cargo ogg_pager 0.7.2 packets/impl#[Packets] { |
| 77 | contentrust-analyzer cargo ogg_pager 0.7.2 packets/Packets#content., |
| 78 | packet_sizesrust-analyzer cargo ogg_pager 0.7.2 packets/Packets#packet_sizes., |
| 79 | }); |
| 80 | } |
| 81 | |
| 82 | let mut packets_read = 0; |
| 83 | |
| 84 | let mut packet_size = 0_u64; |
| 85 | let mut packet_bytes_already_read = Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#; |
| 86 | let mut current_packet_content; |
| 87 | 'outer: while let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(header) = PageHeaderrust-analyzer cargo ogg_pager 0.7.2 header/PageHeader#::readrust-analyzer cargo ogg_pager 0.7.2 header/impl#[PageHeader]read().(data) { |
| 88 | for i in header.segmentsrust-analyzer cargo ogg_pager 0.7.2 header/PageHeader#segments. { |
| 89 | packet_size rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`AddAssign<Self>`]add_assign().u64::fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/num/impl#[u64][`From<u8>`]from().(i); |
| 90 | |
| 91 | if i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialOrd<Self>`]lt().255 { |
| 92 | if count rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[isize][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[isize][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[isize][`PartialEq<Self>`]ne().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[isize][Neg]neg().1 { |
| 93 | packets_read rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[isize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[isize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[isize][`AddAssign<Self>`]add_assign().1; |
| 94 | } |
| 95 | |
| 96 | let byte_count_to_read = |
| 97 | Selfrust-analyzer cargo ogg_pager 0.7.2 packets/impl#[Packets]::get_byte_count_to_readrust-analyzer cargo ogg_pager 0.7.2 packets/impl#[Packets]get_byte_count_to_read().(packet_size, &mut packet_bytes_already_read); |
| 98 | |
| 99 | current_packet_content = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![0; byte_count_to_read as usize]; |
| 100 | data.read_exactrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#read_exact().(&mut current_packet_content)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 101 | |
| 102 | packet_sizes.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(packet_size); |
| 103 | packet_size = 0; |
| 104 | packet_bytes_already_read = Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#; |
| 105 | |
| 106 | content.appendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]append().(&mut current_packet_content); |
| 107 | |
| 108 | if packets_read rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[isize][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[isize][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[isize][`PartialEq<Self>`]eq().count { |
| 109 | break 'outer; |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | // The packet continues on the next page, write what we can so far |
| 115 | if packet_size rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialEq<Self>`]ne().0 { |
| 116 | let byte_count_to_read = |
| 117 | Selfrust-analyzer cargo ogg_pager 0.7.2 packets/impl#[Packets]::get_byte_count_to_readrust-analyzer cargo ogg_pager 0.7.2 packets/impl#[Packets]get_byte_count_to_read().(packet_size, &mut packet_bytes_already_read); |
| 118 | |
| 119 | current_packet_content = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![0; byte_count_to_read as usize]; |
| 120 | data.read_exactrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#read_exact().(&mut current_packet_content)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 121 | content.appendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]append().(&mut current_packet_content); |
| 122 | } |
| 123 | |
| 124 | continue; |
| 125 | } |
| 126 | |
| 127 | if count rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[isize][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[isize][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[isize][`PartialEq<Self>`]ne().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[isize][Neg]neg().1 && packet_sizes.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne().count as usize { |
| 128 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(PageErrorrust-analyzer cargo ogg_pager 0.7.2 error/PageError#::NotEnoughDatarust-analyzer cargo ogg_pager 0.7.2 error/PageError#NotEnoughData#); |
| 129 | } |
| 130 | |
| 131 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Selfrust-analyzer cargo ogg_pager 0.7.2 packets/impl#[Packets] { |
| 132 | contentrust-analyzer cargo ogg_pager 0.7.2 packets/Packets#content., |
| 133 | packet_sizesrust-analyzer cargo ogg_pager 0.7.2 packets/Packets#packet_sizes., |
| 134 | }) |
| 135 | } |
| 136 | |
| 137 | fn get_byte_count_to_readrust-analyzer cargo ogg_pager 0.7.2 packets/impl#[Packets]get_byte_count_to_read().( |
| 138 | packet_size: u64, |
| 139 | packet_bytes_already_read: &mut Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<u64>, |
| 140 | ) -> u64 { |
| 141 | let byte_count_to_read; |
| 142 | match packet_bytes_already_read { |
| 143 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(already_read_bytes_count) => { |
| 144 | byte_count_to_read = packet_size rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Sub<Self>`]sub().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().already_read_bytes_count; |
| 145 | *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().packet_bytes_already_read = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().already_read_bytes_count rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Add<Self>`]add().byte_count_to_read); |
| 146 | }, |
| 147 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => { |
| 148 | byte_count_to_read = packet_size; |
| 149 | *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().packet_bytes_already_read = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(packet_size); |
| 150 | }, |
| 151 | } |
| 152 | |
| 153 | byte_count_to_read |
| 154 | } |
| 155 | |
| 156 | /// Returns the number of packets |
| 157 | /// |
| 158 | /// # Examples |
| 159 | /// |
| 160 | /// ```rust |
| 161 | /// use ogg_pager::Packets; |
| 162 | /// |
| 163 | /// # fn main() -> Result<(), ogg_pager::PageError> { |
| 164 | /// # let path = "../lofty/tests/files/assets/minimal/full_test.ogg"; |
| 165 | /// let mut file = std::fs::File::open(path)?; |
| 166 | /// |
| 167 | /// // I want to read 2 packets |
| 168 | /// let packets = Packets::read_count(&mut file, 2)?; |
| 169 | /// |
| 170 | /// // And that's what I received! |
| 171 | /// assert_eq!(packets.len(), 2); |
| 172 | /// # Ok(()) } |
| 173 | /// ``` |
| 174 | pub fn lenrust-analyzer cargo ogg_pager 0.7.2 packets/impl#[Packets]len().(&self) -> usize { |
| 175 | self.packet_sizesrust-analyzer cargo ogg_pager 0.7.2 packets/Packets#packet_sizes..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() |
| 176 | } |
| 177 | |
| 178 | /// Returns true if there are no packets |
| 179 | /// |
| 180 | /// # Examples |
| 181 | /// |
| 182 | /// ```rust |
| 183 | /// use ogg_pager::Packets; |
| 184 | /// |
| 185 | /// # fn main() -> Result<(), ogg_pager::PageError> { |
| 186 | /// # let path = "../lofty/tests/files/assets/minimal/full_test.ogg"; |
| 187 | /// let mut file = std::fs::File::open(path)?; |
| 188 | /// |
| 189 | /// let packets = Packets::read(&mut file)?; |
| 190 | /// |
| 191 | /// // My file contains packets! |
| 192 | /// assert!(!packets.is_empty()); |
| 193 | /// # Ok(()) } |
| 194 | /// ``` |
| 195 | pub fn is_emptyrust-analyzer cargo ogg_pager 0.7.2 packets/impl#[Packets]is_empty().(&self) -> bool { |
| 196 | self.packet_sizesrust-analyzer cargo ogg_pager 0.7.2 packets/Packets#packet_sizes..is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]is_empty().() |
| 197 | } |
| 198 | |
| 199 | fn packet_spanrust-analyzer cargo ogg_pager 0.7.2 packets/impl#[Packets]packet_span().(&self, idx: usize) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Rangerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#<usize>> { |
| 200 | if idx rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().self.packet_sizesrust-analyzer cargo ogg_pager 0.7.2 packets/Packets#packet_sizes..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() { |
| 201 | return Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#; |
| 202 | } |
| 203 | |
| 204 | let start_pos = match idx { |
| 205 | // Packet 0 starts at pos 0 |
| 206 | 0 => 0, |
| 207 | other => self.packet_sizesrust-analyzer cargo ogg_pager 0.7.2 packets/Packets#packet_sizes.[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#other]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().sumrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#sum().::<u64>() as usize, |
| 208 | }; |
| 209 | |
| 210 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(packet_size) = self.packet_sizesrust-analyzer cargo ogg_pager 0.7.2 packets/Packets#packet_sizes..getrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]get().(idx) { |
| 211 | return Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(start_pos..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#start_pos rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().packet_size as usize); |
| 212 | } |
| 213 | |
| 214 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# |
| 215 | } |
| 216 | |
| 217 | /// Gets the packet at a specified index, returning its contents |
| 218 | /// |
| 219 | /// NOTES: |
| 220 | /// |
| 221 | /// * This is zero-indexed |
| 222 | /// * If the index is out of bounds, it will return [`None`] |
| 223 | /// |
| 224 | /// # Examples |
| 225 | /// |
| 226 | /// ```rust |
| 227 | /// use ogg_pager::Packets; |
| 228 | /// |
| 229 | /// # fn main() -> Result<(), ogg_pager::PageError> { |
| 230 | /// # let path = "../lofty/tests/files/assets/minimal/full_test.ogg"; |
| 231 | /// let mut file = std::fs::File::open(path)?; |
| 232 | /// |
| 233 | /// let packets = Packets::read(&mut file)?; |
| 234 | /// |
| 235 | /// let first_packet = packets.get(0); |
| 236 | /// assert!(first_packet.is_some()); |
| 237 | /// |
| 238 | /// let out_of_bounds = packets.get(1000000); |
| 239 | /// assert!(out_of_bounds.is_none()); |
| 240 | /// # Ok(()) } |
| 241 | /// ``` |
| 242 | pub fn getrust-analyzer cargo ogg_pager 0.7.2 packets/impl#[Packets]get().(&self, idx: usize) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&[u8]> { |
| 243 | self.packet_spanrust-analyzer cargo ogg_pager 0.7.2 packets/impl#[Packets]packet_span().(idx).maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(|range| &self.contentrust-analyzer cargo ogg_pager 0.7.2 packets/Packets#content.[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().range]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().) |
| 244 | } |
| 245 | |
| 246 | /// Sets the packet content, if it exists |
| 247 | /// |
| 248 | /// NOTES: |
| 249 | /// |
| 250 | /// * This is zero-indexed |
| 251 | /// * If the index is out of bounds, it will return `false` |
| 252 | /// |
| 253 | /// # Examples |
| 254 | /// |
| 255 | /// ```rust |
| 256 | /// use ogg_pager::Packets; |
| 257 | /// |
| 258 | /// # fn main() -> Result<(), ogg_pager::PageError> { |
| 259 | /// # let path = "../lofty/tests/files/assets/minimal/full_test.ogg"; |
| 260 | /// let mut file = std::fs::File::open(path)?; |
| 261 | /// |
| 262 | /// let mut packets = Packets::read(&mut file)?; |
| 263 | /// |
| 264 | /// let new_content = [0; 100]; |
| 265 | /// |
| 266 | /// assert_ne!(packets.get(0), Some(new_content.as_slice())); |
| 267 | /// |
| 268 | /// // Set our new content |
| 269 | /// assert!(packets.set(0, new_content)); |
| 270 | /// |
| 271 | /// // Now our packet contains the new content |
| 272 | /// assert_eq!(packets.get(0), Some(new_content.as_slice())); |
| 273 | /// |
| 274 | /// // We cannot index out of bounds |
| 275 | /// assert!(!packets.set(1000000, new_content)); |
| 276 | /// # Ok(()) } |
| 277 | /// ``` |
| 278 | pub fn setrust-analyzer cargo ogg_pager 0.7.2 packets/impl#[Packets]set().(&mut self, idx: usize, content: impl Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>>) -> bool { |
| 279 | let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(range) = self.packet_spanrust-analyzer cargo ogg_pager 0.7.2 packets/impl#[Packets]packet_span().(idx) else { |
| 280 | return false; |
| 281 | }; |
| 282 | |
| 283 | let content = content.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#into().(); |
| 284 | let content_size = content.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().(); |
| 285 | |
| 286 | self.contentrust-analyzer cargo ogg_pager 0.7.2 packets/Packets#content..splicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]splice().(range, content); |
| 287 | self.packet_sizesrust-analyzer cargo ogg_pager 0.7.2 packets/Packets#packet_sizes.[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`IndexMut<I>`]index_mut().idx]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`IndexMut<I>`]index_mut(). = content_size as u64; |
| 288 | |
| 289 | true |
| 290 | } |
| 291 | |
| 292 | /// Returns an iterator over the packets |
| 293 | /// |
| 294 | /// # Examples |
| 295 | /// |
| 296 | /// ```rust |
| 297 | /// use ogg_pager::Packets; |
| 298 | /// |
| 299 | /// # fn main() -> Result<(), ogg_pager::PageError> { |
| 300 | /// # let path = "../lofty/tests/files/assets/minimal/full_test.ogg"; |
| 301 | /// let mut file = std::fs::File::open(path)?; |
| 302 | /// |
| 303 | /// let packets = Packets::read(&mut file)?; |
| 304 | /// |
| 305 | /// for packet in packets.iter() { |
| 306 | /// println!("Packet size: {}", packet.len()); |
| 307 | /// } |
| 308 | /// # Ok(()) } |
| 309 | pub fn iterrust-analyzer cargo ogg_pager 0.7.2 packets/impl#[Packets]iter().(&self) -> PacketsIterrust-analyzer cargo ogg_pager 0.7.2 packets/PacketsIter#<'_> { |
| 310 | <&Selfrust-analyzer cargo ogg_pager 0.7.2 packets/impl#[Packets] as IntoIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#>::into_iterrust-analyzer cargo ogg_pager 0.7.2 packets/impl#[`&'a Packets`][IntoIterator]into_iter().(self) |
| 311 | } |
| 312 | |
| 313 | /// Convert the packets into a stream of pages |
| 314 | /// |
| 315 | /// See [paginate()] for more information. |
| 316 | /// |
| 317 | /// # Errors |
| 318 | /// |
| 319 | /// See [`paginate()`] |
| 320 | /// |
| 321 | /// # Examples |
| 322 | /// |
| 323 | /// ```rust |
| 324 | /// use ogg_pager::{CONTAINS_FIRST_PAGE_OF_BITSTREAM, CONTAINS_LAST_PAGE_OF_BITSTREAM, Packets}; |
| 325 | /// |
| 326 | /// # fn main() -> Result<(), ogg_pager::PageError> { |
| 327 | /// # let path = "../lofty/tests/files/assets/minimal/full_test.ogg"; |
| 328 | /// let mut file = std::fs::File::open(path)?; |
| 329 | /// |
| 330 | /// let mut packets = Packets::read(&mut file)?; |
| 331 | /// |
| 332 | /// let stream_serial_number = 1234; |
| 333 | /// let absolute_granule_position = 0; |
| 334 | /// let flags = CONTAINS_FIRST_PAGE_OF_BITSTREAM | CONTAINS_LAST_PAGE_OF_BITSTREAM; |
| 335 | /// |
| 336 | /// let pages = packets.paginate(stream_serial_number, absolute_granule_position, flags)?; |
| 337 | /// |
| 338 | /// println!("We created {} pages!", pages.len()); |
| 339 | /// # Ok(()) } |
| 340 | /// ``` |
| 341 | pub fn paginaterust-analyzer cargo ogg_pager 0.7.2 packets/impl#[Packets]paginate().(&self, stream_serial: u32, abgp: u64, flags: u8) -> Resultrust-analyzer cargo ogg_pager 0.7.2 error/Result#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Pagerust-analyzer cargo ogg_pager 0.7.2 Page#>> { |
| 342 | let mut packets = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().(); |
| 343 | |
| 344 | let mut pos = 0; |
| 345 | for packet_size in self.packet_sizesrust-analyzer cargo ogg_pager 0.7.2 packets/Packets#packet_sizes..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().copiedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#copied().() { |
| 346 | packets.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(&self.contentrust-analyzer cargo ogg_pager 0.7.2 packets/Packets#content.[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().pos..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#pos rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().packet_size as usize]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().); |
| 347 | pos rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().packet_size as usize; |
| 348 | } |
| 349 | |
| 350 | paginaterust-analyzer cargo ogg_pager 0.7.2 paginate/paginate().(packets, stream_serial, abgp, flags) |
| 351 | } |
| 352 | |
| 353 | /// Write packets to a writer |
| 354 | /// |
| 355 | /// This will paginate and write all of the packets to a writer. |
| 356 | /// |
| 357 | /// # Errors |
| 358 | /// |
| 359 | /// * Unable to write, see [`std::io::Error`] |
| 360 | /// |
| 361 | /// # Examples |
| 362 | /// |
| 363 | /// ```rust,no_run |
| 364 | /// use ogg_pager::{CONTAINS_FIRST_PAGE_OF_BITSTREAM, CONTAINS_LAST_PAGE_OF_BITSTREAM, Packets}; |
| 365 | /// use std::fs::OpenOptions; |
| 366 | /// |
| 367 | /// # fn main() -> Result<(), ogg_pager::PageError> { |
| 368 | /// let mut file = std::fs::File::open("foo.ogg")?; |
| 369 | /// |
| 370 | /// let mut packets = Packets::read(&mut file)?; |
| 371 | /// |
| 372 | /// let stream_serial_number = 1234; |
| 373 | /// let absolute_granule_position = 0; |
| 374 | /// let flags = CONTAINS_FIRST_PAGE_OF_BITSTREAM | CONTAINS_LAST_PAGE_OF_BITSTREAM; |
| 375 | /// |
| 376 | /// let mut new_file = OpenOptions::new().write(true).open("bar.ogg")?; |
| 377 | /// let pages_written = packets.write_to( |
| 378 | /// &mut new_file, |
| 379 | /// stream_serial_number, |
| 380 | /// absolute_granule_position, |
| 381 | /// flags, |
| 382 | /// )?; |
| 383 | /// |
| 384 | /// println!("We wrote {} pages!", pages_written); |
| 385 | /// # Ok(()) } |
| 386 | /// ``` |
| 387 | pub fn write_torust-analyzer cargo ogg_pager 0.7.2 packets/impl#[Packets]write_to().<W>( |
| 388 | &self, |
| 389 | writer: &mut W, |
| 390 | stream_serial: u32, |
| 391 | abgp: u64, |
| 392 | flags: u8, |
| 393 | ) -> Resultrust-analyzer cargo ogg_pager 0.7.2 error/Result#<usize> |
| 394 | where |
| 395 | W: Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#, |
| 396 | { |
| 397 | let paginated = self.paginaterust-analyzer cargo ogg_pager 0.7.2 packets/impl#[Packets]paginate().(stream_serial, abgp, flags)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 398 | let num_pages = paginated.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().(); |
| 399 | |
| 400 | for mut page in paginated { |
| 401 | page.gen_crcrust-analyzer cargo ogg_pager 0.7.2 impl#[Page]gen_crc().(); |
| 402 | writer.write_allrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#write_all().(&page.as_bytesrust-analyzer cargo ogg_pager 0.7.2 impl#[Page]as_bytes().())?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 403 | } |
| 404 | |
| 405 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(num_pages) |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | /// An iterator over packets |
| 410 | /// |
| 411 | /// This is created by calling `into_iter` on [`Packets`] |
| 412 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 413 | pub struct PacketsIterrust-analyzer cargo ogg_pager 0.7.2 packets/PacketsIter#<'a> { |
| 414 | contentrust-analyzer cargo ogg_pager 0.7.2 packets/PacketsIter#content.: &'a [u8], |
| 415 | packet_sizesrust-analyzer cargo ogg_pager 0.7.2 packets/PacketsIter#packet_sizes.: &'a [u64], |
| 416 | caprust-analyzer cargo ogg_pager 0.7.2 packets/PacketsIter#cap.: usize, |
| 417 | } |
| 418 | |
| 419 | impl<'a> Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator# for PacketsIterrust-analyzer cargo ogg_pager 0.7.2 packets/PacketsIter#<'a> { |
| 420 | type Itemrust-analyzer cargo ogg_pager 0.7.2 packets/impl#[`PacketsIter<'a>`][Iterator]Item# = &'a [u8]; |
| 421 | |
| 422 | fn nextrust-analyzer cargo ogg_pager 0.7.2 packets/impl#[`PacketsIter<'a>`][Iterator]next().(&mut self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Selfrust-analyzer cargo ogg_pager 0.7.2 packets/impl#[`PacketsIter<'a>`][Iterator]::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#> { |
| 423 | if self.caprust-analyzer cargo ogg_pager 0.7.2 packets/PacketsIter#cap. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().0 { |
| 424 | return Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#; |
| 425 | } |
| 426 | |
| 427 | let packet_size = self.packet_sizesrust-analyzer cargo ogg_pager 0.7.2 packets/PacketsIter#packet_sizes.[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().; |
| 428 | |
| 429 | self.caprust-analyzer cargo ogg_pager 0.7.2 packets/PacketsIter#cap. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`SubAssign<Self>`]sub_assign().-=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`SubAssign<Self>`]sub_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`SubAssign<Self>`]sub_assign().1; |
| 430 | self.packet_sizesrust-analyzer cargo ogg_pager 0.7.2 packets/PacketsIter#packet_sizes. = &self.packet_sizesrust-analyzer cargo ogg_pager 0.7.2 packets/PacketsIter#packet_sizes.[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().1..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().; |
| 431 | |
| 432 | let (ret, remaining) = self.contentrust-analyzer cargo ogg_pager 0.7.2 packets/PacketsIter#content..split_atrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]split_at().(packet_size as usize); |
| 433 | self.contentrust-analyzer cargo ogg_pager 0.7.2 packets/PacketsIter#content. = remaining; |
| 434 | |
| 435 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(ret) |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | impl<'a> IntoIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator# for &'a Packetsrust-analyzer cargo ogg_pager 0.7.2 packets/Packets# { |
| 440 | type Itemrust-analyzer cargo ogg_pager 0.7.2 packets/impl#[`&'a Packets`][IntoIterator]Item# = &'a [u8]; |
| 441 | type IntoIterrust-analyzer cargo ogg_pager 0.7.2 packets/impl#[`&'a Packets`][IntoIterator]IntoIter# = PacketsIterrust-analyzer cargo ogg_pager 0.7.2 packets/PacketsIter#<'a>; |
| 442 | |
| 443 | fn into_iterrust-analyzer cargo ogg_pager 0.7.2 packets/impl#[`&'a Packets`][IntoIterator]into_iter().(self) -> Selfrust-analyzer cargo ogg_pager 0.7.2 packets/impl#[`&'a Packets`][IntoIterator]::IntoIterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#IntoIter# { |
| 444 | PacketsIterrust-analyzer cargo ogg_pager 0.7.2 packets/PacketsIter# { |
| 445 | contentrust-analyzer cargo ogg_pager 0.7.2 packets/PacketsIter#content.: &self.contentrust-analyzer cargo ogg_pager 0.7.2 packets/Packets#content., |
| 446 | packet_sizesrust-analyzer cargo ogg_pager 0.7.2 packets/PacketsIter#packet_sizes.: &self.packet_sizesrust-analyzer cargo ogg_pager 0.7.2 packets/Packets#packet_sizes., |
| 447 | caprust-analyzer cargo ogg_pager 0.7.2 packets/PacketsIter#cap.: self.packet_sizesrust-analyzer cargo ogg_pager 0.7.2 packets/Packets#packet_sizes..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().(), |
| 448 | } |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | impl Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug# for Packetsrust-analyzer cargo ogg_pager 0.7.2 packets/Packets# { |
| 453 | fn fmtrust-analyzer cargo ogg_pager 0.7.2 packets/impl#[Packets][Debug]fmt().(&self, f: &mut Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#<'_>) -> stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# { |
| 454 | f.debug_structrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`Formatter<'a>`]debug_struct().("Packets") |
| 455 | .fieldrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/builders/impl#[`DebugStruct<'a, 'b>`]field().("total_bytes", &self.contentrust-analyzer cargo ogg_pager 0.7.2 packets/Packets#content..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().()) |
| 456 | .fieldrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/builders/impl#[`DebugStruct<'a, 'b>`]field().("count", &self.packet_sizesrust-analyzer cargo ogg_pager 0.7.2 packets/Packets#packet_sizes..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().()) |
| 457 | .finishrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/builders/impl#[`DebugStruct<'a, 'b>`]finish().() |
| 458 | } |
| 459 | } |