diff --git a/src/common.rs b/src/common.rs index 1cf7aa37c6cd0893d9800635236fa4719b81803d..8fd148c70ac89a5c07a4fefbf7a129bd40fedd55 100644 --- a/src/common.rs +++ b/src/common.rs @@ -29,7 +29,7 @@ pub type ControllerId = u32; pub type ChannelIndex = u32; #[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd)] -pub struct Payload(Vec); +pub struct Payload(Arc>); /// This is a unique identifier for a channel (i.e., port). #[derive(Debug, Eq, PartialEq, Clone, Hash, Copy, Ord, PartialOrd)] @@ -120,7 +120,7 @@ impl Payload { unsafe { v.set_len(len); } - Payload(v) + Payload(Arc::new(v)) } pub fn len(&self) -> usize { self.0.len() @@ -129,12 +129,17 @@ impl Payload { &self.0 } pub fn as_mut_slice(&mut self) -> &mut [u8] { - &mut self.0 + Arc::make_mut(&mut self.0) as _ + } + pub fn concat_with(&mut self, other: &Self) { + let bytes = other.as_slice().iter().copied(); + let me = Arc::make_mut(&mut self.0); + me.extend(bytes); } } impl std::iter::FromIterator for Payload { fn from_iter>(it: I) -> Self { - Self(it.into_iter().collect()) + Self(Arc::new(it.into_iter().collect())) } } impl From> for Payload {