Skip to content

Commit b24571b

Browse files
committed
encode: introduce encoded_length utility function
1 parent 9914169 commit b24571b

File tree

2 files changed

+46
-27
lines changed

2 files changed

+46
-27
lines changed

src/confidential.rs

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,6 @@ impl Value {
7070
Value::Confidential(comm)
7171
}
7272

73-
/// Serialized length, in bytes
74-
pub fn encoded_length(&self) -> usize {
75-
match *self {
76-
Value::Null => 1,
77-
Value::Explicit(..) => 9,
78-
Value::Confidential(..) => 33,
79-
}
80-
}
81-
8273
/// Create from commitment.
8374
pub fn from_commitment(bytes: &[u8]) -> Result<Self, encode::Error> {
8475
Ok(Value::Confidential(PedersenCommitment::from_slice(bytes)?))
@@ -145,13 +136,23 @@ impl Encodable for Value {
145136
Value::Confidential(commitment) => commitment.consensus_encode(&mut s),
146137
}
147138
}
139+
140+
fn encoded_length(&self) -> usize {
141+
match *self {
142+
Self::Null => 1,
143+
Self::Explicit(..) => 9,
144+
Self::Confidential(..) => 33,
145+
}
146+
}
148147
}
149148

150149
impl Encodable for PedersenCommitment {
151150
fn consensus_encode<W: io::Write>(&self, mut e: W) -> Result<usize, encode::Error> {
152151
e.write_all(&self.serialize())?;
153152
Ok(33)
154153
}
154+
155+
fn encoded_length(&self) -> usize { 33 }
155156
}
156157

157158
impl Decodable for Value {
@@ -272,15 +273,6 @@ impl Asset {
272273
))
273274
}
274275

275-
/// Serialized length, in bytes
276-
pub fn encoded_length(&self) -> usize {
277-
match *self {
278-
Asset::Null => 1,
279-
Asset::Explicit(..) => 33,
280-
Asset::Confidential(..) => 33,
281-
}
282-
}
283-
284276
/// Create from commitment.
285277
pub fn from_commitment(bytes: &[u8]) -> Result<Self, encode::Error> {
286278
Ok(Asset::Confidential(Generator::from_slice(bytes)?))
@@ -367,13 +359,23 @@ impl Encodable for Asset {
367359
Asset::Confidential(generator) => generator.consensus_encode(&mut s)
368360
}
369361
}
362+
363+
fn encoded_length(&self) -> usize {
364+
match *self {
365+
Self::Null => 1,
366+
Self::Explicit(..) => 33,
367+
Self::Confidential(..) => 33,
368+
}
369+
}
370370
}
371371

372372
impl Encodable for Generator {
373373
fn consensus_encode<W: io::Write>(&self, mut e: W) -> Result<usize, encode::Error> {
374374
e.write_all(&self.serialize())?;
375375
Ok(33)
376376
}
377+
378+
fn encoded_length(&self) -> usize { 33 }
377379
}
378380

379381
impl Decodable for Asset {
@@ -537,15 +539,6 @@ impl Nonce {
537539
SecretKey::from_slice(&shared_secret[..32]).expect("always has exactly 32 bytes")
538540
}
539541

540-
/// Serialized length, in bytes
541-
pub fn encoded_length(&self) -> usize {
542-
match *self {
543-
Nonce::Null => 1,
544-
Nonce::Explicit(..) => 33,
545-
Nonce::Confidential(..) => 33,
546-
}
547-
}
548-
549542
/// Create from commitment.
550543
pub fn from_commitment(bytes: &[u8]) -> Result<Self, encode::Error> {
551544
Ok(Nonce::Confidential(
@@ -619,13 +612,23 @@ impl Encodable for Nonce {
619612
Nonce::Confidential(commitment) => commitment.consensus_encode(&mut s),
620613
}
621614
}
615+
616+
fn encoded_length(&self) -> usize {
617+
match *self {
618+
Self::Null => 1,
619+
Self::Explicit(..) => 33,
620+
Self::Confidential(..) => 33,
621+
}
622+
}
622623
}
623624

624625
impl Encodable for PublicKey {
625626
fn consensus_encode<W: io::Write>(&self, mut e: W) -> Result<usize, encode::Error> {
626627
e.write_all(&self.serialize())?;
627628
Ok(33)
628629
}
630+
631+
fn encoded_length(&self) -> usize { 33 }
629632
}
630633

631634
impl Decodable for Nonce {

src/encode.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,22 @@ pub trait Encodable {
147147
/// the underlying `Write` errors. Returns the number of bytes written on
148148
/// success
149149
fn consensus_encode<W: io::Write>(&self, e: W) -> Result<usize, Error>;
150+
151+
/// The length of the object, in bytes, when encoded.
152+
#[inline]
153+
fn encoded_length(&self) -> usize {
154+
self.consensus_encode(io::sink()).expect("no errors on sink")
155+
}
156+
157+
/// The length of the object, in bytes, when encoded.
158+
///
159+
/// Convenience function to minimize the amount of explicit casting that
160+
/// users need to do. No object in Bitcoin or Elements will ever approach
161+
/// [`u64::MAX`] in encoded size, regardless of the range of `usize`.
162+
#[inline]
163+
fn encoded_len64(&self) -> u64 {
164+
self.encoded_length() as u64
165+
}
150166
}
151167

152168
/// Data which can be encoded in a consensus-consistent way

0 commit comments

Comments
 (0)