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

fix: (u)int tokenization #123

Merged
merged 4 commits into from
Jun 20, 2023
Merged
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
2 changes: 1 addition & 1 deletion crates/primitives/src/bits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub type B512 = FixedBytes<64>;
impl From<crate::U256> for B256 {
#[inline]
fn from(value: crate::U256) -> Self {
Self::from(value.to_be_bytes())
Self(value.to_be_bytes())
}
}

Expand Down
53 changes: 23 additions & 30 deletions crates/primitives/src/signed/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ pub struct Signed<const BITS: usize, const LIMBS: usize>(pub(crate) Uint<BITS, L

// formatting
impl<const BITS: usize, const LIMBS: usize> fmt::Debug for Signed<BITS, LIMBS> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(self, f)
}
Expand All @@ -88,44 +89,38 @@ impl<const BITS: usize, const LIMBS: usize> fmt::Debug for Signed<BITS, LIMBS> {
impl<const BITS: usize, const LIMBS: usize> fmt::Display for Signed<BITS, LIMBS> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let (sign, abs) = self.into_sign_and_abs();
fmt::Display::fmt(&sign, f)?;
// sign must be formatted directly, instead of with `write!` due to the
// `sign_positive` flag
sign.fmt(f)?;
write!(f, "{abs}")
}
}

impl<const BITS: usize, const LIMBS: usize> fmt::LowerHex for Signed<BITS, LIMBS> {
impl<const BITS: usize, const LIMBS: usize> fmt::Binary for Signed<BITS, LIMBS> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let (sign, abs) = self.into_sign_and_abs();
fmt::Display::fmt(&sign, f)?;
write!(f, "{abs:x}")
self.0.fmt(f)
}
}

impl<const BITS: usize, const LIMBS: usize> fmt::UpperHex for Signed<BITS, LIMBS> {
impl<const BITS: usize, const LIMBS: usize> fmt::Octal for Signed<BITS, LIMBS> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let (sign, abs) = self.into_sign_and_abs();
fmt::Display::fmt(&sign, f)?;

// NOTE: Work around `Uint: !UpperHex`.
let mut buffer = format!("{abs:x}");
buffer.make_ascii_uppercase();
f.write_str(&buffer)
self.0.fmt(f)
}
}

impl<const BITS: usize, const LIMBS: usize> fmt::Binary for Signed<BITS, LIMBS> {
impl<const BITS: usize, const LIMBS: usize> fmt::LowerHex for Signed<BITS, LIMBS> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let (sign, abs) = self.into_sign_and_abs();
fmt::Display::fmt(&sign, f)?;
write!(f, "{abs:b}")
self.0.fmt(f)
}
}

impl<const BITS: usize, const LIMBS: usize> fmt::Octal for Signed<BITS, LIMBS> {
impl<const BITS: usize, const LIMBS: usize> fmt::UpperHex for Signed<BITS, LIMBS> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let (sign, abs) = self.into_sign_and_abs();
fmt::Display::fmt(&sign, f)?;
write!(f, "{abs:o}")
self.0.fmt(f)
}
}

Expand Down Expand Up @@ -518,9 +513,6 @@ mod tests {
type I96 = Signed<96, 2>;
type U96 = Uint<96, 2>;

// TODO: ruint::aliases::U192 is bugged
type U192 = Uint<192, 3>;

#[test]
fn identities() {
macro_rules! test_identities {
Expand Down Expand Up @@ -745,6 +737,7 @@ mod tests {
macro_rules! run_test {
($i_struct:ty, $u_struct:ty) => {
let unsigned = <$u_struct>::from_str_radix("3141592653589793", 10).unwrap();
let unsigned_negative = -unsigned;
let positive = <$i_struct>::try_from(unsigned).unwrap();
let negative = -positive;

Expand All @@ -754,25 +747,25 @@ mod tests {
assert_eq!(format!("{negative:+}"), format!("-{unsigned}"));

assert_eq!(format!("{positive:x}"), format!("{unsigned:x}"));
assert_eq!(format!("{negative:x}"), format!("-{unsigned:x}"));
assert_eq!(format!("{positive:+x}"), format!("+{unsigned:x}"));
assert_eq!(format!("{negative:+x}"), format!("-{unsigned:x}"));
assert_eq!(format!("{negative:x}"), format!("{unsigned_negative:x}"));
assert_eq!(format!("{positive:+x}"), format!("{unsigned:x}"));
assert_eq!(format!("{negative:+x}"), format!("{unsigned_negative:x}"));

assert_eq!(
format!("{positive:X}"),
format!("{unsigned:x}").to_uppercase()
);
assert_eq!(
format!("{negative:X}"),
format!("-{unsigned:x}").to_uppercase()
format!("{unsigned_negative:x}").to_uppercase()
);
assert_eq!(
format!("{positive:+X}"),
format!("+{unsigned:x}").to_uppercase()
format!("{unsigned:x}").to_uppercase()
);
assert_eq!(
format!("{negative:+X}"),
format!("-{unsigned:x}").to_uppercase()
format!("{unsigned_negative:x}").to_uppercase()
);
};
}
Expand Down
3 changes: 3 additions & 0 deletions crates/sol-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ hex.workspace = true
serde = { workspace = true, optional = true, features = ["derive"] }

[dev-dependencies]
alloy-primitives = { workspace = true, features = ["arbitrary"] }

hex-literal.workspace = true
proptest.workspace = true
trybuild = "1.0"

[features]
Expand Down
8 changes: 7 additions & 1 deletion crates/sol-types/src/coder/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
//! - Dynamic-length byte arrays `u8[]`

use crate::{no_std_prelude::*, Decoder, Encoder, Result, Word};
use alloy_primitives::{Address, I256, U256};
use alloy_primitives::{Address, FixedBytes, I256, U256};
use core::fmt;

mod sealed {
Expand Down Expand Up @@ -178,6 +178,12 @@ impl TokenType for WordToken {
}

impl WordToken {
/// Create a new word token from a word.
#[inline]
pub const fn new(array: [u8; 32]) -> Self {
Self(FixedBytes(array))
}

/// Returns a reference to the word as a slice.
#[inline]
pub const fn as_slice(&self) -> &[u8] {
Expand Down
Loading