prberg
August 23, 2023, 6:41pm
1
I was looking at the Address type. Is it possible to define a top-level constant of type Address?
I tried this, but it didn’t work:
pub const foo: Address = Address::from([1; 32]);
I received this error:
error[E0015]: cannot call non-const fn `<Address as From<[u8; 32]>>::from` in constants
--> crates/sablier-core/src/constants.rs:2:26
|
2 | pub const PRB: Address = Address::from([1; 32]);
| ^^^^^^^^^^^^^^^^^^^^^^
|
= note: calls in constants are limited to constant functions, tuple structs and tuple variants
= help: add `#![feature(const_trait_impl)]` to the crate attributes to enable
However, this works:
pub const foo: Address = Address::zeroed();
david
August 23, 2023, 10:50pm
2
I believe there’s no problem with defining a constant address, the problem is the using the from function.
Creating it directly with this should work:
pub const PRB: Address = Address { value: [1; 32] };
The From/Into traits supplied by the Rust stdlib aren’t const compatible. However as a workaround we supply a const constructor for the Address type in the Rust SDK:
Address::new([1u8; 32])
If there are other const operations you’d like to use, you can find them via the auto-generated cargo doc for this type:
prberg
August 24, 2023, 7:22am
4
Thanks, @Voxelot ! The new constructor worked.
But @david ’s suggestion didn’t:
missing structure fields:
- 0
david
August 24, 2023, 9:35am
5
Sorry @prberg , I thought you were coding in Sway, not Rust