Is it possible to define top-level constant addresses?

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();

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:

1 Like

Thanks, @Voxelot! The new constructor worked.

But @david’s suggestion didn’t:

missing structure fields:
- 0

Sorry @prberg, I thought you were coding in Sway, not Rust :slight_smile:

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.