This repository was archived by the owner on Oct 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathencode.rs
More file actions
50 lines (44 loc) · 1.51 KB
/
encode.rs
File metadata and controls
50 lines (44 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Copyright 2019 King's College London.
// Created by the Software Development Team <http://soft-dev.org/>.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use crate::Pack;
use rmp_serde::{encode, Serializer};
use serde::Serialize;
use std::{io::prelude::*, ops::Drop};
/// The pack encoder.
///
/// Packs are written using the `serialise()` method. Once all of the desired packs is serialised,
/// the consumer must call `done()`.
pub struct Encoder<'a> {
ser: Serializer<&'a mut dyn Write>,
done: bool,
}
impl<'a> Encoder<'a> {
/// Creates a new encoder which serialises `Pack` into the writable `write_into`.
pub fn from(write_into: &'a mut dyn Write) -> Self {
let ser = Serializer::new(write_into);
Self { ser, done: false }
}
/// Serialises a pack.
pub fn serialise(&mut self, md: Pack) -> Result<(), encode::Error> {
Some(md).serialize(&mut self.ser)
}
/// Finalises the serialisation and writes a sentinel.
pub fn done(mut self) -> Result<(), encode::Error> {
None::<Option<Pack>>.serialize(&mut self.ser)?;
self.done = true;
Ok(())
}
}
impl<'a> Drop for Encoder<'a> {
fn drop(&mut self) {
if !self.done {
panic!("Encoder not marked done()");
}
}
}