bstr icon indicating copy to clipboard operation
bstr copied to clipboard

Someday: implement `ByteSlice` for `[u8; N]` using const generics

Open thomcc opened this issue 4 years ago • 4 comments

In the distant future, when bstr can use const generics, the following reduces the amount of B(b"blah") you need to to literals do by a decent amount, letting you use ByteSlice methods directly on b"foo".

(Of course, you still need B for the &str literal case, e.g. B("😅") and such, but in practice that case isn't as bad, since as_bytes() also works)

impl<const N: usize> ByteSlice for [u8; N] {
    #[inline]
    fn as_bytes(&self) -> &[u8] {
        self
    }

    #[inline]
    fn as_bytes_mut(&mut self) -> &mut [u8] {
        self
    }
}

(Well, in practice you also need impl<const N: usize> Sealed for [u8; N] {}, but that's not important).

Nothing to do for now, but would be a nice thing to add when (or if, I guess) the MSRV gets to 1.51.

thomcc avatar Apr 13 '21 16:04 thomcc

Oooooo yes, this would be lovely. I freaking hate B. :-)

This will likely motivate to bump the MSRV sooner than I would otherwise. I'll wait until it's at least been on stable for two release though.

BurntSushi avatar Apr 13 '21 16:04 BurntSushi

Would now be a good time for this?

Cyborus04 avatar Sep 23 '21 02:09 Cyborus04

This is unblocked now that bstr 1.0 is out and MSRV is 1.60

lopopolo avatar Sep 14 '22 04:09 lopopolo

PR for this is here:

  • https://github.com/BurntSushi/bstr/pull/133

that was pretty painless!

lopopolo avatar Sep 14 '22 04:09 lopopolo