Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mass renaming, container Display impls #486

Merged
merged 16 commits into from
Mar 16, 2023
6 changes: 3 additions & 3 deletions examples/read_all_values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ fn read_all_values<R: RawReader>(reader: &mut R) -> IonResult<usize> {
Value(ion_type) => {
count += 1;
match ion_type {
Struct | List | SExpression => reader.step_in()?,
Struct | List | SExp => reader.step_in()?,
String => {
reader.map_string(|_s| ())?;
}
Symbol => {
let _symbol_id = reader.read_symbol()?;
}
Integer => {
Int => {
let _int = reader.read_i64()?;
}
Float => {
Expand All @@ -79,7 +79,7 @@ fn read_all_values<R: RawReader>(reader: &mut R) -> IonResult<usize> {
Timestamp => {
let _timestamp = reader.read_timestamp()?;
}
Boolean => {
Bool => {
let _boolean = reader.read_bool()?;
}
Blob => {
Expand Down
6 changes: 3 additions & 3 deletions src/binary/binary_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::types::decimal::Decimal;
use crate::types::timestamp::Timestamp;
use crate::types::SymbolId;
use crate::writer::IonWriter;
use crate::{Integer, IonType, SymbolTable};
use crate::{Int, IonType, SymbolTable};
use delegate::delegate;
use std::io::Write;

Expand Down Expand Up @@ -180,7 +180,7 @@ impl<W: Write> IonWriter for BinaryWriter<W> {
fn write_null(&mut self, ion_type: IonType) -> IonResult<()>;
fn write_bool(&mut self, value: bool) -> IonResult<()>;
fn write_i64(&mut self, value: i64) -> IonResult<()>;
fn write_integer(&mut self, value: &Integer) -> IonResult<()>;
fn write_int(&mut self, value: &Int) -> IonResult<()>;
fn write_f32(&mut self, value: f32) -> IonResult<()>;
fn write_f64(&mut self, value: f64) -> IonResult<()>;
fn write_decimal(&mut self, value: &Decimal) -> IonResult<()>;
Expand Down Expand Up @@ -233,7 +233,7 @@ mod tests {
binary_writer.flush()?;

let mut reader = ReaderBuilder::new().build(buffer)?;
assert_eq!(Value(IonType::Integer), reader.next()?);
assert_eq!(Value(IonType::Int), reader.next()?);
let mut annotations = reader.annotations();
assert_eq!("foo", annotations.next().unwrap()?);
assert_eq!("bar", annotations.next().unwrap()?);
Expand Down
8 changes: 4 additions & 4 deletions src/binary/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{
types::{
coefficient::{Coefficient, Sign},
decimal::Decimal,
integer::UInteger,
integer::UInt,
},
IonError,
};
Expand All @@ -23,7 +23,7 @@ const DECIMAL_BUFFER_SIZE: usize = 32;
const DECIMAL_POSITIVE_ZERO: Decimal = Decimal {
coefficient: Coefficient {
sign: Sign::Positive,
magnitude: UInteger::U64(0),
magnitude: UInt::U64(0),
},
exponent: 0,
};
Expand Down Expand Up @@ -75,8 +75,8 @@ where
} else {
// Otherwise, allocate a Vec<u8> with the necessary representation.
let mut coefficient_bytes = match decimal.coefficient.magnitude() {
UInteger::U64(unsigned) => unsigned.to_be_bytes().into(),
UInteger::BigUInt(big) => big.to_bytes_be(),
UInt::U64(unsigned) => unsigned.to_be_bytes().into(),
UInt::BigUInt(big) => big.to_bytes_be(),
};

let first_byte: &mut u8 = &mut coefficient_bytes[0];
Expand Down
36 changes: 18 additions & 18 deletions src/binary/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::data_source::IonDataSource;
use crate::result::{decoding_error, IonResult};
use crate::types::coefficient;
use crate::types::coefficient::Coefficient;
use crate::Integer;
use crate::Int;
use num_bigint::{BigInt, Sign};
use num_traits::Zero;
use std::io::Write;
Expand All @@ -23,14 +23,14 @@ const MAX_INT_SIZE_IN_BYTES: usize = 2048;
#[derive(Debug)]
pub struct DecodedInt {
size_in_bytes: usize,
value: Integer,
value: Int,
// [Integer] is not capable of natively representing negative zero. We track the sign
// of the value separately so we can distinguish between 0 and -0.
is_negative: bool,
}

impl DecodedInt {
pub(crate) fn new(value: Integer, is_negative: bool, size_in_bytes: usize) -> Self {
pub(crate) fn new(value: Int, is_negative: bool, size_in_bytes: usize) -> Self {
DecodedInt {
size_in_bytes,
value,
Expand All @@ -43,7 +43,7 @@ impl DecodedInt {
if length == 0 {
return Ok(DecodedInt {
size_in_bytes: 0,
value: Integer::I64(0),
value: Int::I64(0),
is_negative: false,
});
} else if length > MAX_INT_SIZE_IN_BYTES {
Expand Down Expand Up @@ -90,7 +90,7 @@ impl DecodedInt {
magnitude <<= 8;
magnitude |= byte;
}
Integer::I64(sign * magnitude)
Int::I64(sign * magnitude)
} else {
// This Int is too big for an i64, we'll need to use a BigInt
let sign: num_bigint::Sign = if buffer[0] & 0b1000_0000 == 0 {
Expand All @@ -104,7 +104,7 @@ impl DecodedInt {
// in the buffer to zero.
buffer[0] &= 0b0111_1111;
let value = BigInt::from_bytes_be(sign, buffer);
Integer::BigInt(value)
Int::BigInt(value)
};

Ok(DecodedInt {
Expand Down Expand Up @@ -154,7 +154,7 @@ impl DecodedInt {

/// Returns the value of the signed integer.
#[inline(always)]
pub fn value(&self) -> &Integer {
pub fn value(&self) -> &Int {
&self.value
}

Expand All @@ -170,13 +170,13 @@ impl DecodedInt {
pub fn zero() -> Self {
DecodedInt {
size_in_bytes: 0,
value: Integer::I64(0),
value: Int::I64(0),
is_negative: false,
}
}
}

impl From<DecodedInt> for Integer {
impl From<DecodedInt> for Int {
/// Note that if the DecodedInt represents -0, converting it to an Integer will result in a 0.
/// If negative zero is significant to your use case, check it using [DecodedInt::is_negative_zero]
/// before converting it to an Integer.
Expand Down Expand Up @@ -206,7 +206,7 @@ impl From<DecodedInt> for Coefficient {
mod tests {
use super::*;
use crate::result::IonResult;
use crate::Integer;
use crate::Int;
use std::io;
use std::io::Cursor;

Expand All @@ -217,23 +217,23 @@ mod tests {
let data = &[0b0011_1100, 0b1000_0111, 0b1000_0001];
let int = DecodedInt::read(&mut Cursor::new(data), data.len()).expect(READ_ERROR_MESSAGE);
assert_eq!(int.size_in_bytes(), 3);
assert_eq!(int.value(), &Integer::I64(3_966_849));
assert_eq!(int.value(), &Int::I64(3_966_849));
}

#[test]
fn test_read_three_byte_negative_int() {
let data = &[0b1011_1100, 0b1000_0111, 0b1000_0001];
let int = DecodedInt::read(&mut Cursor::new(data), data.len()).expect(READ_ERROR_MESSAGE);
assert_eq!(int.size_in_bytes(), 3);
assert_eq!(int.value(), &Integer::I64(-3_966_849));
assert_eq!(int.value(), &Int::I64(-3_966_849));
}

#[test]
fn test_read_int_negative_zero() {
let data = &[0b1000_0000]; // Negative zero
let int = DecodedInt::read(&mut Cursor::new(data), data.len()).expect(READ_ERROR_MESSAGE);
assert_eq!(int.size_in_bytes(), 1);
assert_eq!(int.value(), &Integer::I64(0));
assert_eq!(int.value(), &Int::I64(0));
assert!(int.is_negative_zero());
}

Expand All @@ -242,7 +242,7 @@ mod tests {
let data = &[0b0000_0000]; // Positive zero
let int = DecodedInt::read(&mut Cursor::new(data), data.len()).expect(READ_ERROR_MESSAGE);
assert_eq!(int.size_in_bytes(), 1);
assert_eq!(int.value(), &Integer::I64(0));
assert_eq!(int.value(), &Int::I64(0));
assert!(!int.is_negative_zero());
}

Expand All @@ -251,23 +251,23 @@ mod tests {
let data = &[0b0111_1111, 0b1111_1111];
let int = DecodedInt::read(&mut Cursor::new(data), data.len()).expect(READ_ERROR_MESSAGE);
assert_eq!(int.size_in_bytes(), 2);
assert_eq!(int.value(), &Integer::I64(32_767));
assert_eq!(int.value(), &Int::I64(32_767));
}

#[test]
fn test_read_two_byte_negative_int() {
let data = &[0b1111_1111, 0b1111_1111];
let int = DecodedInt::read(&mut Cursor::new(data), data.len()).expect(READ_ERROR_MESSAGE);
assert_eq!(int.size_in_bytes(), 2);
assert_eq!(int.value(), &Integer::I64(-32_767));
assert_eq!(int.value(), &Int::I64(-32_767));
}

#[test]
fn test_read_int_length_zero() {
let data = &[];
let int = DecodedInt::read(&mut Cursor::new(data), data.len()).expect(READ_ERROR_MESSAGE);
assert_eq!(int.size_in_bytes(), 0);
assert_eq!(int.value(), &Integer::I64(0));
assert_eq!(int.value(), &Int::I64(0));
}

#[test]
Expand Down Expand Up @@ -330,7 +330,7 @@ mod tests {
let mut buffer: Vec<u8> = vec![];
let length = DecodedInt::write_i64(&mut buffer, i64::MAX)?;
let i = DecodedInt::read(&mut io::Cursor::new(buffer.as_slice()), length)?;
assert_eq!(i.value, Integer::I64(i64::MAX));
assert_eq!(i.value, Int::I64(i64::MAX));
Ok(())
}
}