pub struct Packets { /* private fields */ }
Expand description
A container for packets in an OGG file
Implementations§
source§impl Packets
impl Packets
sourcepub fn read_count<R>(data: &mut R, count: isize) -> Result<Self>
pub fn read_count<R>(data: &mut R, count: isize) -> Result<Self>
Read a specific number of packets from a reader
A special value of -1
will read as many packets as possible,
in which case Packets::read
should be used.
NOTE: Any value 0 or below will return an empty Packets
§Errors
- Unable to read the specified number of packets
- A page has a bad length
§Examples
use ogg_pager::Packets;
let mut file = std::fs::File::open(path)?;
// We know that the file has at least 2 packets in it
let packets = Packets::read_count(&mut file, 2)?;
sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of packets
§Examples
use ogg_pager::Packets;
let mut file = std::fs::File::open(path)?;
// I want to read 2 packets
let packets = Packets::read_count(&mut file, 2)?;
// And that's what I received!
assert_eq!(packets.len(), 2);
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if there are no packets
§Examples
use ogg_pager::Packets;
let mut file = std::fs::File::open(path)?;
let packets = Packets::read(&mut file)?;
// My file contains packets!
assert!(!packets.is_empty());
sourcepub fn get(&self, idx: usize) -> Option<&[u8]>
pub fn get(&self, idx: usize) -> Option<&[u8]>
Gets the packet at a specified index, returning its contents
NOTES:
- This is zero-indexed
- If the index is out of bounds, it will return
None
§Examples
use ogg_pager::Packets;
let mut file = std::fs::File::open(path)?;
let packets = Packets::read(&mut file)?;
let first_packet = packets.get(0);
assert!(first_packet.is_some());
let out_of_bounds = packets.get(1000000);
assert!(out_of_bounds.is_none());
sourcepub fn set(&mut self, idx: usize, content: impl Into<Vec<u8>>) -> bool
pub fn set(&mut self, idx: usize, content: impl Into<Vec<u8>>) -> bool
Sets the packet content, if it exists
NOTES:
- This is zero-indexed
- If the index is out of bounds, it will return
false
§Examples
use ogg_pager::Packets;
let mut file = std::fs::File::open(path)?;
let mut packets = Packets::read(&mut file)?;
let new_content = [0; 100];
assert_ne!(packets.get(0), Some(new_content.as_slice()));
// Set our new content
assert!(packets.set(0, new_content));
// Now our packet contains the new content
assert_eq!(packets.get(0), Some(new_content.as_slice()));
// We cannot index out of bounds
assert!(!packets.set(1000000, new_content));
sourcepub fn iter(&self) -> PacketsIter<'_> ⓘ
pub fn iter(&self) -> PacketsIter<'_> ⓘ
Returns an iterator over the packets
§Examples
use ogg_pager::Packets;
let mut file = std::fs::File::open(path)?;
let packets = Packets::read(&mut file)?;
for packet in packets.iter() {
println!("Packet size: {}", packet.len());
}
sourcepub fn paginate(
&self,
stream_serial: u32,
abgp: u64,
flags: u8
) -> Result<Vec<Page>>
pub fn paginate( &self, stream_serial: u32, abgp: u64, flags: u8 ) -> Result<Vec<Page>>
Convert the packets into a stream of pages
See paginate() for more information.
§Errors
See paginate()
§Examples
use ogg_pager::{Packets, CONTAINS_FIRST_PAGE_OF_BITSTREAM, CONTAINS_LAST_PAGE_OF_BITSTREAM};
let mut file = std::fs::File::open(path)?;
let mut packets = Packets::read(&mut file)?;
let stream_serial_number = 1234;
let absolute_granule_position = 0;
let flags = CONTAINS_FIRST_PAGE_OF_BITSTREAM | CONTAINS_LAST_PAGE_OF_BITSTREAM;
let pages = packets.paginate(stream_serial_number, absolute_granule_position, flags)?;
println!("We created {} pages!", pages.len());
sourcepub fn write_to<W>(
&self,
writer: &mut W,
stream_serial: u32,
abgp: u64,
flags: u8
) -> Result<usize>where
W: Write,
pub fn write_to<W>(
&self,
writer: &mut W,
stream_serial: u32,
abgp: u64,
flags: u8
) -> Result<usize>where
W: Write,
Write packets to a writer
This will paginate and write all of the packets to a writer.
§Errors
- Unable to write, see
std::io::Error
§Examples
use ogg_pager::{Packets, CONTAINS_FIRST_PAGE_OF_BITSTREAM, CONTAINS_LAST_PAGE_OF_BITSTREAM};
use std::fs::OpenOptions;
let mut file = std::fs::File::open("foo.ogg")?;
let mut packets = Packets::read(&mut file)?;
let stream_serial_number = 1234;
let absolute_granule_position = 0;
let flags = CONTAINS_FIRST_PAGE_OF_BITSTREAM | CONTAINS_LAST_PAGE_OF_BITSTREAM;
let mut new_file = OpenOptions::new().write(true).open("bar.ogg")?;
let pages_written = packets.write_to(
&mut new_file,
stream_serial_number,
absolute_granule_position,
flags,
)?;
println!("We wrote {} pages!", pages_written);
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Packets
impl RefUnwindSafe for Packets
impl Send for Packets
impl Sync for Packets
impl Unpin for Packets
impl UnwindSafe for Packets
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more