Struct ogg_pager::Packets

source ·
pub struct Packets { /* private fields */ }
Expand description

A container for packets in an OGG file

Implementations§

source§

impl Packets

source

pub fn read<R>(data: &mut R) -> Result<Self>
where R: Read + Seek,

Read as many packets as possible from a reader

§Errors

A page has a bad length

§Examples
use ogg_pager::Packets;

let mut file = std::fs::File::open(path)?;

let packets = Packets::read(&mut file)?;
source

pub fn read_count<R>(data: &mut R, count: isize) -> Result<Self>
where R: Read + Seek,

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)?;
source

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);
source

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());
source

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());
source

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));
source

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());
}
source

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());
source

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
§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§

source§

impl Debug for Packets

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'a> IntoIterator for &'a Packets

§

type Item = &'a [u8]

The type of the elements being iterated over.
§

type IntoIter = PacketsIter<'a>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.