Closed
Description
There is apparently no way to construct unsized struct. This can be worked around with transmute
, but only if the struct only has one field (the unsized one). Even with a single field, there should be a way to do this without unsafe
.
Test case:
pub struct Foo {
bytes: [u8]
}
impl Foo {
fn new(bytes: &[u8]) -> &Foo {
&Foo { bytes: *bytes }
}
}
Output:
a.rs:7:10: 7:31 error: the trait `core::kinds::Sized` is not implemented for the type `Foo`
a.rs:7 &Foo { bytes: *bytes }
^~~~~~~~~~~~~~~~~~~~~
a.rs:7:10: 7:31 note: structs must have a statically known size to be initialized
a.rs:7 &Foo { bytes: *bytes }
^~~~~~~~~~~~~~~~~~~~~
A work-around:
fn new(bytes: &[u8]) -> &Foo {
unsafe { ::std::mem::transmute(bytes) }
}
Metadata
Metadata
Assignees
Labels
No labels