-
Notifications
You must be signed in to change notification settings - Fork 57
feat(rad): new RadonInteger operators #2650
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -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, | ||||||||
| }, | ||||||||
| }; | ||||||||
|
|
||||||||
|
|
@@ -20,6 +20,20 @@ pub fn absolute(input: &RadonInteger) -> Result<RadonInteger, RadError> { | |||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| pub fn to_bytes(input: RadonInteger) -> Result<RadonBytes, RadError> { | ||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
I know you followed what |
||||||||
| let mut bytes_array = [0u8; 16]; | ||||||||
| bytes_array.copy_from_slice(&input.value().to_be_bytes()); | ||||||||
|
Comment on lines
+24
to
+25
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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())) | ||||||||
| } | ||||||||
|
|
||||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -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) | ||||||
| } | ||||||
|
|
@@ -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) | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can stick to
Suggested change
|
||||||
| } | ||||||
| (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(), | ||||||
|
|
||||||
There was a problem hiding this comment.
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_bytesmethod would take the preferred endianness as an argument (even if we default to Big Endian)@guidiaz What do you think?