Skip to content

Commit dd8dbfa

Browse files
authored
der: make Reader cloneable (#1883)
Alternative to #1882 which makes arbitrary peeking possible by directly cloning the `Reader`, rather than providing a facade which clones it for you. This also makes `Reader::peek_into` a provided method.
1 parent 5806c27 commit dd8dbfa

File tree

3 files changed

+11
-17
lines changed

3 files changed

+11
-17
lines changed

der/src/reader.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,13 @@ use crate::{
1616
use alloc::vec::Vec;
1717

1818
/// Reader trait which reads DER-encoded input.
19-
pub trait Reader<'r>: Sized {
19+
pub trait Reader<'r>: Clone {
2020
/// Get the [`EncodingRules`] which should be applied when decoding the input.
2121
fn encoding_rules(&self) -> EncodingRules;
2222

2323
/// Get the length of the input.
2424
fn input_len(&self) -> Length;
2525

26-
/// Peek at the decoded PEM without updating the internal state, writing into the provided
27-
/// output buffer.
28-
///
29-
/// Attempts to fill the entire buffer, returning an error if there is not enough data.
30-
fn peek_into(&self, buf: &mut [u8]) -> crate::Result<()>;
31-
3226
/// Get the position within the buffer.
3327
fn position(&self) -> Length;
3428

@@ -121,6 +115,16 @@ pub trait Reader<'r>: Sized {
121115
self.peek_into(&mut byte).ok().map(|_| byte[0])
122116
}
123117

118+
/// Peek at the decoded data without updating the internal state, writing into the provided
119+
/// output buffer.
120+
///
121+
/// Attempts to fill the entire buffer, returning an error if there is not enough data.
122+
fn peek_into(&self, buf: &mut [u8]) -> Result<(), Error> {
123+
let mut reader = self.clone();
124+
reader.read_into(buf)?;
125+
Ok(())
126+
}
127+
124128
/// Peek forward in the input data, attempting to decode a [`Header`] from
125129
/// the data at the current position in the decoder.
126130
///

der/src/reader/pem.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,6 @@ impl<'i> Reader<'i> for PemReader<'i> {
5151
self.position.input_len()
5252
}
5353

54-
fn peek_into(&self, buf: &mut [u8]) -> Result<()> {
55-
self.clone().read_into(buf)?;
56-
Ok(())
57-
}
58-
5954
fn position(&self) -> Length {
6055
self.position.current()
6156
}

der/src/reader/slice.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,6 @@ impl<'a> Reader<'a> for SliceReader<'a> {
8484
self.bytes.len()
8585
}
8686

87-
fn peek_into(&self, buf: &mut [u8]) -> crate::Result<()> {
88-
self.clone().read_into(buf)?;
89-
Ok(())
90-
}
91-
9287
fn position(&self) -> Length {
9388
self.position
9489
}

0 commit comments

Comments
 (0)