Skip to content

Commit

Permalink
Make key_len and encoding_len_varint const (#1199)
Browse files Browse the repository at this point in the history
* feat(prost): Make encoding_len_varint a const fn

* feat(prost): Make key_len a const fn
  • Loading branch information
mzabaluev authored Dec 6, 2024
1 parent 653e1d0 commit 38533ef
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
4 changes: 2 additions & 2 deletions prost/src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ pub fn decode_key(buf: &mut impl Buf) -> Result<(u32, WireType), DecodeError> {
/// Returns the width of an encoded Protobuf field key with the given tag.
/// The returned width will be between 1 and 5 bytes (inclusive).
#[inline]
pub fn key_len(tag: u32) -> usize {
encoded_len_varint(u64::from(tag << 3))
pub const fn key_len(tag: u32) -> usize {
encoded_len_varint((tag << 3) as u64)
}

/// Helper function which abstracts reading a length delimiter prefix followed
Expand Down
2 changes: 1 addition & 1 deletion prost/src/encoding/varint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub fn encode_varint(mut value: u64, buf: &mut impl BufMut) {
/// Returns the encoded length of the value in LEB128 variable length format.
/// The returned value will be between 1 and 10, inclusive.
#[inline]
pub fn encoded_len_varint(value: u64) -> usize {
pub const fn encoded_len_varint(value: u64) -> usize {
// Based on [VarintSize64][1].
// [1]: https://github.com/protocolbuffers/protobuf/blob/v28.3/src/google/protobuf/io/coded_stream.h#L1744-L1756
// Safety: value | 1 is non-zero.
Expand Down

0 comments on commit 38533ef

Please sign in to comment.