Does this code exhibit UB?

I ran it through miri, but it doesn't have much to say.

I used an LLM to rename some types in a source file -- it did as told but also said that write_into_bytesmut() has UB.

Your write_into_bytesmut() is unsound. That is, it does not always exhibit undefined behavior, but if called by safe code in a particular way, it will. There are two problems:

  1. If buf is not large enough, you write past its end (buffer overflow).
  2. You are taking potentially uninitialized memory and creating an &mut [u8] that points to it. Whether or not this is immediate UB (this decision is still being debated), it is definitely unsound to pass that &mut [u8] to IntoBytes::write_to(), which makes no guarantee that it doesn't read the u8s pointed to (even though that would be pointless).

Instead, you can use IntoBytes::as_bytes() to get a &[u8] to the data, then use BytesMut::extend_from_slice() to copy the data. This is entirely safe code and no less efficient.

Ah, so that's the reason the LLM was rather forceful in emphasizing that creating a &mut [u8] to uninitialized memory is insta-UB.

The LLM is wrong (also, don't trust LLMs, definitely not for such things). It is undecided, and furthermore, it is my understanding that most T-opsem members want this to be allowed. You should refrain from doing that until it's decided though.

Also, like @kpreid said, passing it to unknown code is definitely unsound.