rustduck-core 1.1.0

Safe typed TSDuck core bindings for rustduck
Documentation
//! Public bounded memory-input endpoint API.

use std::num::NonZeroUsize;
use std::sync::Arc;

use crate::{MemoryPushOutcome, MemoryQueuePolicy, Result};

use super::binding::MemoryInputBinding;
use super::inner::MemoryInputInner;
use super::state::{acquire, take};
use super::validation::{capacity, packets};
use super::{CallbackError, PacketQueue};

/// Thread-safe bounded input endpoint for a TSDuck `memory` plugin.
///
/// Every payload must contain complete 188-byte TS packets. `close` causes a
/// bound input callback to report EOF after draining queued data; `abort`
/// wakes blocked producers/callbacks and fails the transfer.
pub struct MemoryInput {
    pub(crate) inner: Arc<MemoryInputInner>,
}

impl MemoryInput {
    /// Creates a bounded input endpoint with a capacity in complete TS packets.
    pub fn new(capacity_bytes: NonZeroUsize, policy: MemoryQueuePolicy) -> Result<Arc<Self>> {
        let inner = MemoryInputInner::new(PacketQueue::new(capacity(capacity_bytes)?, policy))?;
        Ok(Arc::new(Self { inner }))
    }

    /// Copies a packet-aligned input slice into queue-owned storage.
    pub fn push(&self, data: &[u8]) -> Result<MemoryPushOutcome> {
        packets(data)?;
        self.inner.queue().push_owned(data.to_vec(), "MemoryInput")
    }

    /// Moves packet-aligned input storage into the queue.
    ///
    /// A complete owned queue head can move directly into the next native input
    /// callback response, avoiding a Rust-side reassembly allocation.
    pub fn push_owned(&self, data: Vec<u8>) -> Result<MemoryPushOutcome> {
        packets(&data)?;
        self.inner.queue().push_owned(data, "MemoryInput")
    }

    /// Shares immutable packet-aligned storage with the queue.
    pub fn push_shared(&self, data: Arc<[u8]>) -> Result<MemoryPushOutcome> {
        packets(&data)?;
        self.inner.queue().push_shared(data, "MemoryInput")
    }

    /// Closes the input after currently queued packets are consumed.
    pub fn close(&self) {
        self.inner.queue().close();
    }
    /// Aborts the input and wakes all blocked operations.
    pub fn abort(&self) {
        self.inner.queue().abort();
    }
    /// Returns queued input bytes.
    pub fn queued_bytes(&self) -> usize {
        self.inner.queue().queued_bytes()
    }
    /// Returns bytes rejected by the drop policy.
    pub fn dropped_bytes(&self) -> usize {
        self.inner.queue().dropped_bytes()
    }
    /// Takes the latest private callback error.
    pub fn take_callback_error(&self) -> Option<CallbackError> {
        take(self.inner.state())
    }

    pub(crate) fn bind(&self) -> Result<MemoryInputBinding> {
        acquire(self.inner.state(), "MemoryInput")?;
        Ok(MemoryInputBinding::new(Arc::clone(&self.inner)))
    }
}