diff options
Diffstat (limited to 'src/transport/mod.rs')
| -rw-r--r-- | src/transport/mod.rs | 26 |
1 files changed, 25 insertions, 1 deletions
diff --git a/src/transport/mod.rs b/src/transport/mod.rs index a040993..0d761e0 100644 --- a/src/transport/mod.rs +++ b/src/transport/mod.rs @@ -326,13 +326,37 @@ pub struct SyncEnvelope { /// ignores an epoch older than the newest it has seen in the same session. pub epoch_seq: u32, #[serde(with = "crate::transport::wire::codec::sync_message")] - pub message: Message, + pub message: SyncPayload, /// The sender was ready when it generated the message: its document is the swarm's, /// not one it is still filling (joining, rejoining). Only such a peer can tell a /// catching-up node that it has everything; an empty document says nothing else. pub ready: bool, } +/// An Automerge sync message in Automerge's own encoding. Decoded only where it is +/// applied (`decode`), after the sender proved to be a member and off the consumer: a +/// large message from anyone else costs nothing but its bytes. +#[derive(Clone)] +pub struct SyncPayload(pub bytes::Bytes); + +impl SyncPayload { + pub fn encode(message: Message) -> Self { + Self(message.encode().into()) + } + + pub fn decode(&self) -> Result<Message> { + Message::decode(&self.0) + .map_err(|error| anyhow::anyhow!("Automerge decode failed: {error:?}")) + } +} + +/// Sizes instead of contents: logged messages stay short. +impl std::fmt::Debug for SyncPayload { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "SyncPayload({} bytes)", self.0.len()) + } +} + /// A message serialized for the wire. Cloning is cheap (shared buffer). #[derive(Clone, Debug)] pub struct EncodedMessage { |
