Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions rad/src/operators/integer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use serde_cbor::value::{Value, from_value};
use crate::{
error::RadError,
types::{
RadonType, boolean::RadonBoolean, float::RadonFloat, integer::RadonInteger,
string::RadonString,
RadonType, boolean::RadonBoolean, bytes::RadonBytes, float::RadonFloat,
integer::RadonInteger, string::RadonString,
},
};

Expand All @@ -20,6 +20,20 @@ pub fn absolute(input: &RadonInteger) -> Result<RadonInteger, RadError> {
}
}

pub fn to_bytes(input: RadonInteger) -> Result<RadonBytes, RadError> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I always assumed that an eventual Integer::to_bytes method would take the preferred endianness as an argument (even if we default to Big Endian)

@guidiaz What do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, it is better to receive the argument by reference to avoid redundant cloning:

Suggested change
pub fn to_bytes(input: RadonInteger) -> Result<RadonBytes, RadError> {
pub fn to_bytes(input: &RadonInteger) -> Result<RadonBytes, RadError> {

I know you followed what to_float and to_string had in place, but that's not needed anymore with modern Rust 💪

let mut bytes_array = [0u8; 16];
bytes_array.copy_from_slice(&input.value().to_be_bytes());
Comment on lines +24 to +25
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should work just as well and benefit from lesser allocation and runtime:

Suggested change
let mut bytes_array = [0u8; 16];
bytes_array.copy_from_slice(&input.value().to_be_bytes());
let bytes_array = input.value().to_be_bytes();

let mut leading_zeros = 0;
for charcode in bytes_array {
if charcode != 0u8 {
break;
} else {
leading_zeros += 1;
}
}
Ok(RadonBytes::from(bytes_array[leading_zeros..].to_vec()))
}

pub fn to_float(input: RadonInteger) -> Result<RadonFloat, RadError> {
RadonFloat::try_from(Value::Integer(input.value()))
}
Expand Down
7 changes: 3 additions & 4 deletions rad/src/operators/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,15 @@ pub enum RadonOpCodes {
///////////////////////////////////////////////////////////////////////
// Integer operator codes (start at 0x40)
IntegerAbsolute = 0x40,
IntegerAsFloat = 0x41,
IntegerAsString = 0x42,
IntegerGreaterThan = 0x43,
IntegerLessThan = 0x44,
// IntegerMatch = 0x45,
IntegerModulo = 0x46,
IntegerMultiply = 0x47,
IntegerNegate = 0x48,
IntegerPower = 0x49,
// IntegerReciprocal = 0x4A,
IntegerToBytes = 0x4A,
IntegerToFloat = 0x41,
IntegerToString = 0x42,
// IntegerSum = 0x4B,
///////////////////////////////////////////////////////////////////////
// Float operator codes (start at 0x50)
Expand Down
14 changes: 8 additions & 6 deletions rad/src/types/integer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,6 @@ impl Operable for RadonInteger {
(RadonOpCodes::IntegerAbsolute, None) => {
integer_operators::absolute(self).map(RadonTypes::from)
}
(RadonOpCodes::IntegerAsFloat, None) => {
integer_operators::to_float(self.clone()).map(RadonTypes::from)
}
(RadonOpCodes::IntegerAsString, None) => {
integer_operators::to_string(self.clone()).map(RadonTypes::from)
}
(RadonOpCodes::IntegerGreaterThan, Some(args)) => {
integer_operators::greater_than(self, args).map(Into::into)
}
Expand All @@ -129,6 +123,14 @@ impl Operable for RadonInteger {
(RadonOpCodes::IntegerPower, Some(args)) => {
integer_operators::power(self, args.as_slice()).map(Into::into)
}
(RadonOpCodes::IntegerToBytes, None) => integer_operators::to_bytes(self.clone())
.map(RadonTypes::from),
(RadonOpCodes::IntegerToFloat, None) => {
integer_operators::to_float(self.clone()).map(RadonTypes::from)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can stick to Into::into for uniformity:

Suggested change
integer_operators::to_float(self.clone()).map(RadonTypes::from)
integer_operators::to_float(self.clone()).map(Into::into)

}
(RadonOpCodes::IntegerToString, None) => {
integer_operators::to_string(self.clone()).map(RadonTypes::from)
}
// Unsupported / unimplemented
(op_code, args) => Err(RadError::UnsupportedOperator {
input_type: RADON_INTEGER_TYPE_NAME.to_string(),
Expand Down
Loading