dope-tls 0.1.1

TCP TLS streams for dope runtime, shin-backed
Documentation
pub struct WireBufs {
    pub plaintext: Vec<u8>,
    pub wire: Vec<u8>,
    pub inflight: Option<Box<[u8]>>,
    pub close: bool,
}

impl WireBufs {
    pub fn new() -> Self {
        Self {
            plaintext: Vec::with_capacity(4096),
            wire: Vec::with_capacity(4096),
            inflight: None,
            close: false,
        }
    }

    #[inline(always)]
    pub fn wire_slice(&self) -> &[u8] {
        &self.wire
    }

    #[inline(always)]
    pub fn take_plaintext(&mut self) -> Vec<u8> {
        std::mem::take(&mut self.plaintext)
    }

    #[inline(always)]
    pub fn install_plaintext(&mut self, mut plaintext: Vec<u8>) {
        if self.plaintext.is_empty() {
            plaintext.clear();
            self.plaintext = plaintext;
        }
    }

    pub fn take_inflight(&mut self) -> Option<(*const u8, u32)> {
        if self.inflight.is_some() {
            return None;
        }
        if self.wire.is_empty() {
            return None;
        }
        let wire_taken = std::mem::take(&mut self.wire).into_boxed_slice();
        let ptr = wire_taken.as_ptr();
        let len = wire_taken.len() as u32;
        self.inflight = Some(wire_taken);
        Some((ptr, len))
    }

    pub fn consume_inflight(&mut self, n: usize) -> bool {
        let Some(chunk) = self.inflight.take() else {
            return false;
        };
        if n < chunk.len() {
            let remaining = &chunk[n..];
            let mut new_wire = Vec::with_capacity(remaining.len() + self.wire.len());
            new_wire.extend_from_slice(remaining);
            new_wire.extend_from_slice(&self.wire);
            self.wire = new_wire;
        }
        !self.wire.is_empty()
    }

    #[inline(always)]
    pub fn has_inflight(&self) -> bool {
        self.inflight.is_some()
    }

    #[inline(always)]
    pub fn close_pending(&self) -> bool {
        self.close
    }
}

impl Default for WireBufs {
    fn default() -> Self {
        Self::new()
    }
}