use std::sync::Arc;
use yoke::trait_hack::YokeTraitHack;
use yoke::{Yoke, Yokeable};
pub struct Owned<Y: for<'a> Yokeable<'a>> {
yoke: Yoke<Y, Arc<[u8]>>,
}
impl<Y: for<'a> Yokeable<'a>> Owned<Y> {
pub fn try_new<F, E>(bytes: Arc<[u8]>, parse: F) -> Result<Self, E>
where
F: for<'a> FnOnce(&'a [u8]) -> Result<<Y as Yokeable<'a>>::Output, E>,
{
Ok(Self {
yoke: Yoke::try_attach_to_cart(bytes, parse)?,
})
}
pub fn new<F>(bytes: Arc<[u8]>, parse: F) -> Self
where
F: for<'a> FnOnce(&'a [u8]) -> <Y as Yokeable<'a>>::Output,
{
Self {
yoke: Yoke::attach_to_cart(bytes, parse),
}
}
#[must_use]
pub fn get(&self) -> &<Y as Yokeable<'_>>::Output {
self.yoke.get()
}
#[must_use]
pub fn backing_bytes(&self) -> &[u8] {
self.yoke.backing_cart()
}
}
impl<Y: for<'a> Yokeable<'a>> Clone for Owned<Y>
where
for<'a> YokeTraitHack<<Y as Yokeable<'a>>::Output>: Clone,
{
fn clone(&self) -> Self {
Self {
yoke: self.yoke.clone(),
}
}
}
impl<Y> std::fmt::Debug for Owned<Y>
where
Y: for<'a> Yokeable<'a>,
for<'a> <Y as Yokeable<'a>>::Output: std::fmt::Debug,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Owned").field("view", self.get()).finish()
}
}
#[doc(hidden)]
pub mod doc {
#[must_use]
pub fn pmt_section() -> Vec<u8> {
use crate::tables::pmt::{Pmt, PmtStream};
use dvb_common::Serialize;
let pmt = Pmt {
program_number: 1,
version_number: 0,
current_next_indicator: true,
pcr_pid: 0x0100,
program_info: crate::descriptors::DescriptorLoop::new(&[]),
streams: vec![PmtStream {
stream_type: 0x1B, elementary_pid: 0x0101,
es_info: crate::descriptors::DescriptorLoop::new(&[]),
}],
};
let mut section = vec![0u8; pmt.serialized_len()];
pmt.serialize_into(&mut section).unwrap();
section
}
}