Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move extending a Captured into a CapturedBuilder. #46

Merged
merged 2 commits into from
Mar 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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 <rpki@nlnetlabs.nl>"]
description = "Handling of data encoded in BER, CER, and DER."
Expand Down
5 changes: 5 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
76 changes: 63 additions & 13 deletions src/captured.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -62,9 +62,9 @@ impl Captured {
/// with the given mode, and returns the resulting data as a captured
/// value.
pub fn from_values<V: encode::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.
Expand All @@ -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<V: encode::Values>(&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.
Expand Down Expand Up @@ -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<V: encode::Values>(&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<Captured> 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<usize, io::Error> {
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down