Skip to content

remove memblock in favor of AsRef, AsMut & Default

Tom Dohrmann requested to merge Freax13/fixed-buffer-rs:as-ref-as-mut into main

Using the traits in rusts standard library is probably more idiomatic:

  • removed the MemBlock trait in favor of AsRef<[u8]> & AsMut<[u8]>
    • this also allows having buffers that are only readable or writable
  • removed the OwnedMemBlock trait in favor of Default
    • unfortunately the Default trait isn't implemented for all array sizes (yet), so you'd need to use new_with_mem for that instead

Unfortunately Box<[0; SIZE]> doesn't implement AsRef<[u8]>, but Box<[u8]> does. This loses a bit of its expressiveness as the size is no longer in the type name, but shouldn't have any big performance penalties even though the size can not always be statically known. This also means that you have to use new_with_mem as Default is not implemented for Box<[u8]>.

On the plus side this allows for a lot more underlying buffers such as u8 arrays of any size as well as dynamically sized u8 arrays, Box<[u8]>, Vec<u8>, etc... and (mutable) references to any of those.

  • removed new_with_slice as it's identical to new_with_mem

  • I didn't update any of the documentation

  • This would obviously be a breaking change

Edited by Tom Dohrmann

Merge request reports