sway icon indicating copy to clipboard operation
sway copied to clipboard

Typed Pointers and Slices

Open AlicanC opened this issue 2 years ago • 2 comments

Previously on Sway, we have introduced raw_ptr and raw_slice types as better alternatives to using u64 and (u64,u64). While we eventually wanted to get rid of these types, they helped identify cases where we play with raw memory and organize our code.

Experiments with their typed variants, struct ptr<T> and struct slice<T>, have shown that they significantly improve core- and std-lib code.

With typed variants:

  • We can leverage the type info to offer more methods, reducing implementation complexity of RawVec<T> and such.
  • All this complexity will move to intrinsics and reduce a lot of bloat. (See: https://github.com/FuelLabs/sway/issues/3112#issuecomment-1292356870)
  • We will be able to remove raw_ptr and raw_slice, making Sway safer.
  • I think we can even remove std::alloc and std::mem.

AlicanC avatar Mar 29 '23 10:03 AlicanC

Some references:

  • https://github.com/FuelLabs/sway/issues/2551
  • https://github.com/FuelLabs/sway/pull/2828
  • https://github.com/FuelLabs/sway/issues/2900
  • https://github.com/FuelLabs/sway/issues/3086
  • https://github.com/FuelLabs/sway/pull/3157
  • https://github.com/FuelLabs/sway/pull/3251
  • https://github.com/FuelLabs/sway/pull/3288

AlicanC avatar Mar 29 '23 10:03 AlicanC

A naive, user-land implementation of typed pointers and its usage can be seen here: https://github.com/FuelLabs/sway/pull/4497/commits/2ca868e2cbfcc1eac2faf1625fee374ef2449198

AlicanC avatar Apr 25 '23 11:04 AlicanC