Skip to content

Commit

Permalink
Address reviewer comments
Browse files Browse the repository at this point in the history
No breaking changes to the API, attributes formatted consistently.

Signed-off-by: Nick Cameron <nrc@ncameron.org>
  • Loading branch information
nrc committed Jun 10, 2019
1 parent 0b09543 commit f1ab424
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
2 changes: 1 addition & 1 deletion benches/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ where
b.iter(|| {
for buf in &payload {
message.clear();
message.merge(buf, DecodeContext::default()).unwrap();
message.merge(buf).unwrap();
criterion::black_box(&message);
}
})
Expand Down
6 changes: 3 additions & 3 deletions src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,13 +184,13 @@ where
/// to a function which is decoding a nested object, then use `enter_recursion`.
#[derive(Clone, Debug)]
pub struct DecodeContext {
#[cfg(not(feature = "no-recursion-limit"))]
/// How many times we can recurse in the current decode stack before we hit
/// the recursion limit.
///
/// The recursion limit is defined by `RECURSION_LIMIT` and cannot be
/// customized. The recursion limit can be ignored by building the Prost
/// crate with the `no-recursion-limit` feature (which is set by default).
#[cfg(not(feature = "no-recursion-limit"))]
recurse_count: u32,
}

Expand All @@ -211,12 +211,12 @@ impl Default for DecodeContext {
}

impl DecodeContext {
#[cfg(not(feature = "no-recursion-limit"))]
/// Call this function before recursively decoding.
///
/// There is no `exit` function since this function creates a new `DecodeContext`
/// to be used at the next level of recursion. Continue to use the old context
// at the previous level of recursion.
#[cfg(not(feature = "no-recursion-limit"))]
#[inline(always)]
pub(crate) fn enter_recursion(&self) -> DecodeContext {
DecodeContext {
Expand All @@ -230,12 +230,12 @@ impl DecodeContext {
DecodeContext {}
}

#[cfg(not(feature = "no-recursion-limit"))]
/// Checks whether the recursion limit has been reached in the stack of
/// decodes described by the `DecodeContext` at `self.ctx`.
///
/// Returns `Ok<()>` if it is ok to continue recursing.
/// Returns `Err<DecodeError>` if the recursion limit has been reached.
#[cfg(not(feature = "no-recursion-limit"))]
#[inline(always)]
pub(crate) fn limit_reached(&self) -> Result<(), DecodeError> {
if self.recurse_count == 0 {
Expand Down
17 changes: 15 additions & 2 deletions src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ pub trait Message: Debug + Send + Sync {
Self: Default,
{
let mut message = Self::default();
Self::merge(&mut message, &mut buf.into_buf(), DecodeContext::default()).map(|_| message)
Self::merge(&mut message, &mut buf.into_buf()).map(|_| message)
}

/// Decodes a length-delimited instance of the message from the buffer.
Expand All @@ -101,7 +101,20 @@ pub trait Message: Debug + Send + Sync {
/// Decodes an instance of the message from a buffer, and merges it into `self`.
///
/// The entire buffer will be consumed.
fn merge<B>(&mut self, buf: B, ctx: DecodeContext) -> Result<(), DecodeError>
fn merge<B>(&mut self, buf: B) -> Result<(), DecodeError>
where
B: IntoBuf,
Self: Sized,
{
self.merge_with_context(buf, DecodeContext::default())
}

/// Decodes an instance of the message from a buffer, and merges it into `self`.
/// This version takes a `DecodeContext` which contains some state for the
/// current decoding. Most users will want to use `merge` without the context.
///
/// The entire buffer will be consumed.
fn merge_with_context<B>(&mut self, buf: B, ctx: DecodeContext) -> Result<(), DecodeError>
where
B: IntoBuf,
Self: Sized,
Expand Down

0 comments on commit f1ab424

Please sign in to comment.