tokio-rs / bytes

Utilities for working with bytes
MIT License
1.87k stars 278 forks source link

Add a safe way to create UninitSlice from slices #598

Closed mina86 closed 1 year ago

mina86 commented 1 year ago

Introduce UninitSlice::from_slice and UninitSlice::from_uninit_slice methods which safely create Uninit slice from provided slice of maybe uninitialised or initialised memory.

In addition, add From<&mut [T]> implementations (for T=u8 and T=MaybeUninit<u8>) which do conversion from slice to UninitSlice.

Fixes: https://github.com/tokio-rs/bytes/issues/552

mina86 commented 1 year ago

By the way, ReadBuf calls those new and uninit. As much as I hate ReadBuf, maybe it’s worth unifying naming with it?

Darksonn commented 1 year ago

Ah, that could make sense.

mina86 commented 1 year ago

Hmm, then again, BorrowedBuf doesn’t have constructors at all and uses From trait. So maybe dropping those constructors would be even better? I have no preference here; wondering what would be best convention.

Darksonn commented 1 year ago

No, I think it is good practice to have constructors.

mina86 commented 1 year ago

https://github.com/tokio-rs/bytes/pull/599