RWLock Wrapper - doesn't have a size known at compile-time

I have some code I found on this forum that I am hoping will help me diagnose a deadlock
by printing to the console on write() and drop

The code is at

I would like to ad an identifier to the RWLock struct.
Ideally this would be a static str of some kind but when I try to add that or even a u8
like

pub struct RwLock<T: ?Sized> {
    pub lock: sync::RwLock<T>,
    id: u8,
}

I start getting ```
pub lock: sync::RwLock,
| ^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time


isn't a u8 known at compile time ? 
Can someone explain how I would get around this and why the error occurs.

Thanks

There can only be one unsized field in a struct, and it must be the last one, even though Rust otherwise reserves the right to reorder fields. Move the declaration of id before the unsized lock and it will work.

1 Like

Thanks. Sounds like a place the compiler output could be better.

You're right. The compiler should make this explicit. The reason as why this rule is true is not hard to conceive, however, this feature would be good for beginners

The last note in full error message does state this restriction:

error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
 --> src/lib.rs:2:5
  |
2 |     data: [u8],
  |     ^^^^^^^^^^ doesn't have a size known at compile-time
  |
  = help: the trait `std::marker::Sized` is not implemented for `[u8]`
  = note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
  = note: only the last field of a struct may have a dynamically sized type

Maybe the error could be refined in this particular case, with only one unsized field but not the last.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.