core::mem::align_of
requires Self: Sized
, even though it only needs to know the type's alignment. Notably, slices have an alignment known at compile time, even though they are not Sized
. Should Rust have an Aligned
trait (as a supertrait of Sized
), so this bound can be loosened?
It makes sense to me, but do you have a practical example where you would have used this?
I'm trying to write a Vec
-like type that can store unsized values (slices) as elements. I would prefer for my Vec
to start out with an aligned dangling pointer, so that I don't need to change said pointer until I necessary. It's a minor annoyance, which I can easily work around, but still nice to have.
Is there an example of !Aligned
type, other than extern types?
dyn Trait
So if it's essentially Vec<[T]>
, can't you use align_of::<T>()
for that pointer? You could also initialize that pointing to [T; 0]
, not even dangling.
I could, hence why I stated it's "easy to work around". Though this becomes less fun if you also want to support structs with a slice as their last field.
Yeah, custom slice-based DSTs are more interesting.
Language-wise, ?Sized
constraints are special, but I suppose that would also have to imply ?Aligned
for backward compatibility. And vice versa, ?Aligned
must imply ?Sized
for the supertrait. You would explicitly combine T: Aligned + ?Sized
for dealing with the middle ground.
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.