How to get a buffer type with the right size and alignment for a type?

Sorry, I typed too fast, I mean to return by reference.

fn get<'a>(&'a self) -> &'a Iter<'a, T>

I need to take a longer look at yoke, but I'm curious where it sits in the constellation of rental, OwningRef, and reffers. I know rental is abandoned, and OwningRef has unmerged pull requests fixing soundness holes so I think it's also abandoned, but it seems like all of these are trying to solve the same problem and I don't entirely understand why this yak is getting shaved so much? Are these kinds of crates always doomed to hit soundness holes and get abandoned? Or is this just rapid evolution as lang features get added? I did some googling around this problem and before this thread hadn't heard of yoke.

New features introducing undefined behavior retrospectively would be really bad, and I think both the community and the teams are wary of doing this, as it can have potentially devastating consequences. I suspect that trying to work around the borrow checker is harder to do soundly than people naïvely assume, and it's certainly harder to do soundly than finding a safe alternative.

1 Like

I thought you want to transform [T;N] to [u8;N*size_of::<T>()]
But, as you see, size_of::<T>() would get error you list.
So, we can tansform [T;N] => &[T] => &[u8]
But, you should check these thing

  1. T is a Sized type
  2. you should know the size_of and align_of of T
  3. T is not a pointer
    If you have checked all the thing, maybe std::mem::transmute is a way.
fn make_buffer<const N:usize, T:Default+Copy>() -> [T;N]  {
    [T::default();N]
}

fn trans_type<T>(arr: &[T])->&[u8]{
    unsafe{
        std::mem::transmute(arr)
    }
}

That's bad, bad advice. Layout of Rust types is not guaranteed unless explicitly noted. You shouldn't therefore transmute between references, either; and this applies even more to fat references. Use raw pointers and their .cast() method instead.

1 Like

Yes, you are right.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.