diff --git a/src/common.rs b/src/common.rs index 91e4d451c504b9112ebaaa1bb1180b4eecb37ab2..1cf7aa37c6cd0893d9800635236fa4719b81803d 100644 --- a/src/common.rs +++ b/src/common.rs @@ -24,10 +24,13 @@ pub use Polarity::*; ///////////////////// DEFS ///////////////////// -pub type Payload = Vec; +// pub type Payload = Vec; pub type ControllerId = u32; pub type ChannelIndex = u32; +#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd)] +pub struct Payload(Vec); + /// This is a unique identifier for a channel (i.e., port). #[derive(Debug, Eq, PartialEq, Clone, Hash, Copy, Ord, PartialOrd)] pub struct ChannelId { @@ -111,6 +114,34 @@ pub trait PolyContext { } ///////////////////// IMPL ///////////////////// +impl Payload { + pub fn new(len: usize) -> Payload { + let mut v = Vec::with_capacity(len); + unsafe { + v.set_len(len); + } + Payload(v) + } + pub fn len(&self) -> usize { + self.0.len() + } + pub fn as_slice(&self) -> &[u8] { + &self.0 + } + pub fn as_mut_slice(&mut self) -> &mut [u8] { + &mut self.0 + } +} +impl std::iter::FromIterator for Payload { + fn from_iter>(it: I) -> Self { + Self(it.into_iter().collect()) + } +} +impl From> for Payload { + fn from(s: Vec) -> Self { + Self(s.into()) + } +} impl Debug for Port { fn fmt(&self, f: &mut Formatter) -> std::fmt::Result { write!(f, "Port({})", self.0)