cairo icon indicating copy to clipboard operation
cairo copied to clipboard

feat: add mutable methods to the storage `Vec` type

Open enitrat opened this issue 1 year ago • 3 comments

Feature Request

Describe the Feature Request

The Vec type is for now an append-only data structure. We can only append at the end, and retrieve existing values.

We could also add:

  • set
  • delete / unset (reset value at index i to default value)
  • pop

I can help if needed.

enitrat avatar Aug 20 '24 17:08 enitrat

Proposal: also improve the API so that one can do

self.storage_vec.at(i) instead of self.storage_vec.at(i).read() self.storage_vec.append(x) instead of self.storage_vec.append().write(x)

enitrat avatar Aug 21 '24 12:08 enitrat

this is a breaking change - additionally - it merges the concepts of finding a Vec cell, and the actual edit of one, which makes it much less explicit.

orizi avatar Aug 21 '24 14:08 orizi

append is quite misleading in this case, it's connoted as mutating a data structure.

Perhaps something like this would be better:

self.storage_vec.last_mut()-> returns the storage path to the last storage cell of the vec, so that we can do self.storage_vec.last_mut().write(x) (or self.storage_vec.last_mut().read()).

And thus, the features I would like to see could be expressed as

self.storage_vec.get_mut(i).write(x) self.storage_vec.get_mut(i).delete()

enitrat avatar Aug 21 '24 15:08 enitrat