Skip to content

Commit

Permalink
Incorporates review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
zslayton committed Jan 12, 2024
1 parent 2569c88 commit bb0fb00
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 12 deletions.
22 changes: 14 additions & 8 deletions src/lazy/encoder/binary/v1_1/fixed_int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ pub struct FixedInt {
}

impl FixedInt {
pub fn new(size_in_bytes: usize, value: impl Into<Int>) -> Self {
fn new(size_in_bytes: usize, value: impl Into<Int>) -> Self {
Self {
value: value.into(),
size_in_bytes,
}
}

/// Reads a [`FixedInt`] from the buffer.
/// Reads a [`FixedInt`] from the beginning of `input`.
///
/// `input` is the byte slice from which to read a [`FixedInt`].
/// `size_in_bytes` is the number of bytes to interpret as an unsigned integer.
Expand Down Expand Up @@ -51,12 +51,7 @@ impl FixedInt {

#[inline]
fn write_i64<W: Write>(output: &mut W, value: i64) -> IonResult<usize> {
let num_magnitude_bits = if value < 0 {
64 - value.leading_ones()
} else {
64 - value.leading_zeros()
};
let num_encoded_bytes = (num_magnitude_bits as usize / 8) + 1;
let num_encoded_bytes = Self::encoded_size_i64(value);

let le_bytes = value.to_le_bytes();
let encoded_bytes = &le_bytes[..num_encoded_bytes];
Expand All @@ -78,6 +73,17 @@ impl FixedInt {
}
}

#[inline]
pub fn encoded_size_i64(value: i64) -> usize {
let num_sign_bits = if value < 0 {
value.leading_ones()
} else {
value.leading_zeros()
};
let num_magnitude_bits = 64 - num_sign_bits;
(num_magnitude_bits as usize / 8) + 1
}

pub fn value(&self) -> &Int {
&self.value
}
Expand Down
4 changes: 2 additions & 2 deletions src/lazy/encoder/binary/v1_1/fixed_uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ pub struct FixedUInt {
}

impl FixedUInt {
pub fn new(size_in_bytes: usize, value: impl Into<UInt>) -> Self {
fn new(size_in_bytes: usize, value: impl Into<UInt>) -> Self {
Self {
value: value.into(),
size_in_bytes,
}
}

/// Reads a [`FixedUInt`] from the buffer.
/// Reads a [`FixedUInt`] from the beginning of `input`.
///
/// `input` is the byte slice from which to read a [`FixedUInt`].
/// `size_in_bytes` is the number of bytes to interpret as an unsigned integer.
Expand Down
2 changes: 1 addition & 1 deletion src/lazy/encoder/binary/v1_1/flex_int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub struct FlexInt {
}

impl FlexInt {
pub fn new(size_in_bytes: usize, value: i64) -> Self {
fn new(size_in_bytes: usize, value: i64) -> Self {
Self {
value,
size_in_bytes,
Expand Down
2 changes: 1 addition & 1 deletion src/lazy/encoder/binary/v1_1/flex_uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub struct FlexUInt {
}

impl FlexUInt {
pub fn new(size_in_bytes: usize, value: u64) -> Self {
fn new(size_in_bytes: usize, value: u64) -> Self {
Self {
value,
size_in_bytes,
Expand Down

0 comments on commit bb0fb00

Please sign in to comment.