From 2153c66d1bb6bb796a4dc96258b7139cd424bca7 Mon Sep 17 00:00:00 2001 From: Martin Hoffmann Date: Wed, 4 Mar 2020 14:15:46 +0100 Subject: [PATCH 1/2] Move extending a Captured into a CapturedBuilder. --- Cargo.toml | 2 +- src/captured.rs | 76 ++++++++++++++++++++++++++++++++++++++++--------- src/lib.rs | 2 +- 3 files changed, 65 insertions(+), 15 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 27b0c1a..0dd16b7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "bcder" -version = "0.4.3-pre" +version = "0.5.0-pre" edition = "2018" authors = ["The NLnet Labs RPKI Team "] description = "Handling of data encoded in BER, CER, and DER." diff --git a/src/captured.rs b/src/captured.rs index b9ec5c5..db03682 100644 --- a/src/captured.rs +++ b/src/captured.rs @@ -3,7 +3,7 @@ //! This is a private module. Its public items are re-exported by the parent. use std::{fmt, io, ops}; -use bytes::Bytes; +use bytes::{Bytes, BytesMut}; use crate::{decode, encode}; use crate::mode::Mode; @@ -62,9 +62,9 @@ impl Captured { /// with the given mode, and returns the resulting data as a captured /// value. pub fn from_values(mode: Mode, values: V) -> Self { - let mut res = Self::new(Bytes::new(), mode); - res.extend(values); - res + let mut builder = Self::builder(mode); + builder.extend(values); + builder.freeze() } /// Creates a new empty captured value in the given mode. @@ -75,15 +75,17 @@ impl Captured { } } - /// Extends the captured value by encoding the given values. + /// Crates a builder for a captured value in the given mode. + pub fn builder(mode: Mode) -> CapturedBuilder { + CapturedBuilder::new(mode) + } + + /// Converts the captured values into a builder in order to add new values. /// - /// The function encodes the given values in the captured value’s own mode - /// and places the encoded content at the end of the captured value. - pub fn extend(&mut self, values: V) { - values.write_encoded( - self.mode, - &mut CapturedWriter(&mut self.bytes) - ).unwrap() + /// Because the captured values might be shared, this requires copying the + /// underlying data. + pub fn into_builder(self) -> CapturedBuilder { + self.into() } /// Decodes the full content using the provided function argument. @@ -188,9 +190,57 @@ impl fmt::Debug for Captured { } +//------------ CapturedBuilder ----------------------------------------------- + +pub struct CapturedBuilder { + bytes: BytesMut, + mode: Mode, +} + +impl CapturedBuilder { + pub fn new(mode: Mode) -> Self { + CapturedBuilder { + bytes: BytesMut::new(), + mode + } + } + + pub fn with_capacity(capacity: usize, mode: Mode) -> Self { + CapturedBuilder { + bytes: BytesMut::with_capacity(capacity), + mode + } + } + + /// Extends the captured value by encoding the given values. + /// + /// The function encodes the given values in the captured value’s own mode + /// and places the encoded content at the end of the captured value. + pub fn extend(&mut self, values: V) { + values.write_encoded( + self.mode, + &mut CapturedWriter(&mut self.bytes) + ).unwrap() + } + + pub fn freeze(self) -> Captured { + Captured::new(self.bytes.freeze(), self.mode) + } +} + +impl From for CapturedBuilder { + fn from(src: Captured) -> Self { + CapturedBuilder { + bytes: src.bytes.as_ref().into(), + mode: src.mode + } + } +} + + //------------ CapturedWriter ------------------------------------------------ -struct CapturedWriter<'a>(&'a mut Bytes); +struct CapturedWriter<'a>(&'a mut BytesMut); impl<'a> io::Write for CapturedWriter<'a> { fn write(&mut self, buf: &[u8]) -> Result { diff --git a/src/lib.rs b/src/lib.rs index c6cc9df..948524d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -36,7 +36,7 @@ //--- Re-exports -pub use self::captured::Captured; +pub use self::captured::{Captured, CapturedBuilder}; pub use self::int::{Integer, Unsigned}; pub use self::mode::Mode; pub use self::oid::{ConstOid, Oid}; From 3235a2cf43af449ca25d78dac571355459d1d5ab Mon Sep 17 00:00:00 2001 From: Martin Hoffmann Date: Wed, 4 Mar 2020 14:35:20 +0100 Subject: [PATCH 2/2] Update Changelog. --- Changelog.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Changelog.md b/Changelog.md index ecc2caf..4fc8432 100644 --- a/Changelog.md +++ b/Changelog.md @@ -4,12 +4,17 @@ Breaking +* Move extending a `Captured` to an explicit `CapturedBuilder`. This + becomes necessary with bytes 0.5. [(#46)] + New Bug Fixes Dependencies +[(#46)]: https://github.com/NLnetLabs/bcder/pull/46 + ## 0.4.2