38 releases

new 0.8.12 Nov 12, 2024
0.8.10 Oct 28, 2024
0.7.7 Jul 8, 2024
0.7.0 Mar 30, 2024
0.3.1 Jul 30, 2023

#488 in Magic Beans

Download history 59292/week @ 2024-07-28 71096/week @ 2024-08-04 82119/week @ 2024-08-11 66133/week @ 2024-08-18 79906/week @ 2024-08-25 91634/week @ 2024-09-01 82656/week @ 2024-09-08 76545/week @ 2024-09-15 86473/week @ 2024-09-22 88078/week @ 2024-09-29 111605/week @ 2024-10-06 107244/week @ 2024-10-13 117190/week @ 2024-10-20 103329/week @ 2024-10-27 111389/week @ 2024-11-03 95567/week @ 2024-11-10

432,600 downloads per month
Used in 332 crates (167 directly)

MIT/Apache

385KB
8K SLoC

alloy-primitives

Primitive types shared by alloy, foundry, revm, and reth.

Types

  • Unsigned integers re-exported from ruint
  • Signed integers, as a wrapper around ruint integers
  • Fixed-size byte arrays via FixedBytes
    • wrap_fixed_bytes!: macro for constructing named fixed bytes types
    • Address, which is a fixed-size byte array of 20 bytes, with EIP-55 and EIP-1191 checksum support
    • fixed_bytes!, address! and other macros to construct the types at compile time

Examples

This library has straightforward, basic, types. Usage is correspondingly simple. Please consult the documentation for more information.

use alloy_primitives::{address, fixed_bytes, Address, FixedBytes, I256, U256};

// FixedBytes
let n: FixedBytes<6> = fixed_bytes!("1234567890ab");
assert_eq!(n, "0x1234567890ab".parse::<FixedBytes<6>>().unwrap());
assert_eq!(n.to_string(), "0x1234567890ab");

// Uint
let mut n: U256 = "42".parse().unwrap();
n += U256::from(10);
assert_eq!(n.to_string(), "52");

// Signed
let mut n: I256 = "-42".parse().unwrap();
n = -n;
assert_eq!(n.to_string(), "42");

// Address
let addr_str = "0x66f9664f97F2b50F62D13eA064982f936dE76657";
let addr: Address = Address::parse_checksummed(addr_str, None).unwrap();
assert_eq!(addr, address!("66f9664f97F2b50F62D13eA064982f936dE76657"));
assert_eq!(addr.to_checksum(None), addr_str);

// Address checksummed with a custom chain id
let addr_str = "0x66F9664f97f2B50F62d13EA064982F936de76657";
let addr: Address = Address::parse_checksummed(addr_str, Some(30)).unwrap();
assert_eq!(addr, address!("66F9664f97f2B50F62d13EA064982F936de76657"));
assert_eq!(addr.to_checksum(Some(30)), addr_str);

Dependencies

~3.5–7MB
~140K SLoC