cairo icon indicating copy to clipboard operation
cairo copied to clipboard

feat: Storage{Base}Address impls

Open enitrat opened this issue 2 years ago • 2 comments

  • Implements the Into trait to convert a Storage{Base}Address into a felt252
  • Implements the Into trait to convert a Storage{Base}Address into a u256
  • Implements PartialEq trait to allow comparison of StorageBaseAddress and StorageAddress

Resolves #4195


This change is Reviewable

enitrat avatar Oct 04 '23 02:10 enitrat

I was thinking that adding them gradually to the corelibrary would be great. It's a bit frustrating to always have to doublecheck whether a type conversion is in the corelib or not, only to end up writing it ourselves locally (and it could benefit other users! I believe common types from the corelib should be implement type conversion traits).

Perhaps I can make the process more generic and write XIntoY where X can be converted to felt252 and felt252 can be converted to Y; and same for TryInto. I don't know if it's possible, though.

That would actually solve most of these cases

Edit: Naively tried this, but it fails with Inference cycle detected. I guess it's also going to be a problem for when converting from X to Y can be done directly and not through a felt.

impl XIntoY<X, +Into<X, felt252>, Y, +Into<felt252, Y>> of Into<X, Y> {
    fn into(self: X) -> Y {
        let self: felt252 = self.into();
        let self: Y = self.into();
        self
    }
}

enitrat avatar Oct 05 '23 02:10 enitrat

If you don't want these kind of implementations in the core library then I can add them to Alexandria instead.

I understand that it might increase considerably the size of the corelib if we start implementing all traits for corelib types. However, it's very convenient, devX wise, to be able to natively perform all of these operations without having to think about where all myimpl are defined, and explicitly importing them

enitrat avatar Oct 05 '23 05:10 enitrat

Fixed by #5534

enitrat avatar Jun 26 '24 12:06 enitrat