Add ExactSizeEncoder impl for Option<T: ExactSizeEncoder>
What changed, and why it matters
This commit adds a small, harmless feature to a Rust Bitcoin encoding library. It lets an optional encoder report its length when it has a value, and report zero length when it is empty. There is no security issue here.
No security action needed. Treat as routine feature addition.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch implements ExactSizeEncoder for Option
Changed components
consensus_encoding/src/encode/mod.rsconsensus_encoding/tests/encode.rsconsensus_encoding/api/*.txtInspect captured patch +17 / −0
diff --git a/consensus_encoding/api/all-features.txt b/consensus_encoding/api/all-features.txt
index e9269ef3..929d9c0b 100644
--- a/consensus_encoding/api/all-features.txt
+++ b/consensus_encoding/api/all-features.txt
@@ -1559,6 +1559,8 @@ pub fn core::option::Option<T>::current_chunk(&self) -> &[u8]
pub trait bitcoin_consensus_encoding::ExactSizeEncoder: bitcoin_consensus_encoding::Encoder
pub fn bitcoin_consensus_encoding::ExactSizeEncoder::is_empty(&self) -> bool
pub fn bitcoin_consensus_encoding::ExactSizeEncoder::len(&self) -> usize
+impl<T: bitcoin_consensus_encoding::ExactSizeEncoder> bitcoin_consensus_encoding::ExactSizeEncoder for core::option::Option<T>
+pub fn core::option::Option<T>::len(&self) -> usize
pub fn bitcoin_consensus_encoding::check_decode<T: bitcoin_consensus_encoding::Decode + core::cmp::Eq + core::fmt::Debug>(bytes: &[u8], expected: &T) where <<T as bitcoin_consensus_encoding::Decode>::Decoder as bitcoin_consensus_encoding::Decoder>::Error: core::fmt::Debug
pub fn bitcoin_consensus_encoding::check_decoder<D: bitcoin_consensus_encoding::Decoder>(decoder: D, bytes: &[u8], expected: &<D as bitcoin_consensus_encoding::Decoder>::Output) where <D as bitcoin_consensus_encoding::Decoder>::Output: core::cmp::Eq + core::fmt::Debug, <D as bitcoin_consensus_encoding::Decoder>::Error: core::fmt::Debug
pub fn bitcoin_consensus_encoding::check_encode<T: bitcoin_consensus_encoding::Encode + ?core::marker::Sized>(value: &T, expected: &[u8])
diff --git a/consensus_encoding/api/alloc-only.txt b/consensus_encoding/api/alloc-only.txt
index cac5a73a..f97dd876 100644
--- a/consensus_encoding/api/alloc-only.txt
+++ b/consensus_encoding/api/alloc-only.txt
@@ -1436,6 +1436,8 @@ pub fn core::option::Option<T>::current_chunk(&self) -> &[u8]
pub trait bitcoin_consensus_encoding::ExactSizeEncoder: bitcoin_consensus_encoding::Encoder
pub fn bitcoin_consensus_encoding::ExactSizeEncoder::is_empty(&self) -> bool
pub fn bitcoin_consensus_encoding::ExactSizeEncoder::len(&self) -> usize
+impl<T: bitcoin_consensus_encoding::ExactSizeEncoder> bitcoin_consensus_encoding::ExactSizeEncoder for core::option::Option<T>
+pub fn core::option::Option<T>::len(&self) -> usize
pub fn bitcoin_consensus_encoding::check_decode<T: bitcoin_consensus_encoding::Decode + core::cmp::Eq + core::fmt::Debug>(bytes: &[u8], expected: &T) where <<T as bitcoin_consensus_encoding::Decode>::Decoder as bitcoin_consensus_encoding::Decoder>::Error: core::fmt::Debug
pub fn bitcoin_consensus_encoding::check_decoder<D: bitcoin_consensus_encoding::Decoder>(decoder: D, bytes: &[u8], expected: &<D as bitcoin_consensus_encoding::Decoder>::Output) where <D as bitcoin_consensus_encoding::Decoder>::Output: core::cmp::Eq + core::fmt::Debug, <D as bitcoin_consensus_encoding::Decoder>::Error: core::fmt::Debug
pub fn bitcoin_consensus_encoding::check_encode<T: bitcoin_consensus_encoding::Encode + ?core::marker::Sized>(value: &T, expected: &[u8])
diff --git a/consensus_encoding/api/no-features.txt b/consensus_encoding/api/no-features.txt
index bd1355f9..eec61103 100644
--- a/consensus_encoding/api/no-features.txt
+++ b/consensus_encoding/api/no-features.txt
@@ -1127,6 +1127,8 @@ pub fn core::option::Option<T>::current_chunk(&self) -> &[u8]
pub trait bitcoin_consensus_encoding::ExactSizeEncoder: bitcoin_consensus_encoding::Encoder
pub fn bitcoin_consensus_encoding::ExactSizeEncoder::is_empty(&self) -> bool
pub fn bitcoin_consensus_encoding::ExactSizeEncoder::len(&self) -> usize
+impl<T: bitcoin_consensus_encoding::ExactSizeEncoder> bitcoin_consensus_encoding::ExactSizeEncoder for core::option::Option<T>
+pub fn core::option::Option<T>::len(&self) -> usize
pub fn bitcoin_consensus_encoding::check_decode<T: bitcoin_consensus_encoding::Decode + core::cmp::Eq + core::fmt::Debug>(bytes: &[u8], expected: &T) where <<T as bitcoin_consensus_encoding::Decode>::Decoder as bitcoin_consensus_encoding::Decoder>::Error: core::fmt::Debug
pub fn bitcoin_consensus_encoding::check_decoder<D: bitcoin_consensus_encoding::Decoder>(decoder: D, bytes: &[u8], expected: &<D as bitcoin_consensus_encoding::Decoder>::Output) where <D as bitcoin_consensus_encoding::Decoder>::Output: core::cmp::Eq + core::fmt::Debug, <D as bitcoin_consensus_encoding::Decoder>::Error: core::fmt::Debug
pub fn bitcoin_consensus_encoding::check_encode<T: bitcoin_consensus_encoding::Encode + ?core::marker::Sized>(value: &T, expected: &[u8])
diff --git a/consensus_encoding/src/encode/mod.rs b/consensus_encoding/src/encode/mod.rs
index 58b6cf64..79b9f93e 100644
--- a/consensus_encoding/src/encode/mod.rs
+++ b/consensus_encoding/src/encode/mod.rs
@@ -488,3 +488,12 @@ impl<T: Encoder> Encoder for Option<T> {
}
}
}
+
+impl<T: ExactSizeEncoder> ExactSizeEncoder for Option<T> {
+ fn len(&self) -> usize {
+ match self {
+ Some(encoder) => encoder.len(),
+ None => 0,
+ }
+ }
+}
diff --git a/consensus_encoding/tests/encode.rs b/consensus_encoding/tests/encode.rs
index 3007f9b0..bad49090 100644
--- a/consensus_encoding/tests/encode.rs
+++ b/consensus_encoding/tests/encode.rs
@@ -204,12 +204,14 @@ fn encode_encoder2_with_first_empty_encoder() {
#[test]
fn encode_option_encoder_some() {
let mut encoder = Some(ArrayEncoder::<3>::without_length_prefix([1, 2, 3]));
+ assert_eq!(encoder.len(), 3);
check_encoder(&mut encoder, &[1, 2, 3]);
}
#[test]
fn encode_option_encoder_none() {
let mut encoder: Option<ArrayEncoder<3>> = None;
+ assert_eq!(encoder.len(), 0);
check_encoder(&mut encoder, &[]);
}
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.