I am trying to create a struct which holds multiple blocks of bytes where each block of bytes is held in an individual RwLock
.
This is for a voxel engine where performance is very important. Each block of bytes will need to be read/written by multiple threads. Multiple RwLocks
are needed so that only a specific block will be locked leaving the rest of the blocks free to be read/written by other threads; locking the full struct would cause locking of all threads that are performing work.
Most other structs will be assigned a specific slot, the block of bytes needs to be stack allocated.
The compiler complains that it cannot copy the RwLock
s as there is no Copy
trait on RwLock
, which I don't want to copy but instance multiple RwLocks
.
mod constants {
pub const CHUNK_BASE_SIZE: usize = 7;
pub const CHUNK_ALLOC_COUNT: usize = 11;
}
mod example {
use std::sync::RwLock;
use super::constants;
// =====================================================================
struct BU8 {
bytes: [[u8; constants::CHUNK_BASE_SIZE]; constants::CHUNK_BASE_SIZE],
}
impl BU8 {
pub fn new() -> BU8 {
BU8 {
bytes: [[0; constants::CHUNK_BASE_SIZE]; constants::CHUNK_BASE_SIZE],
}
}
}
// =====================================================================
pub struct Bytes {
block: RwLock<BU8>,
}
impl Bytes {
pub fn new() -> Bytes {
Bytes {
block: RwLock::new(BU8::new()),
}
}
pub fn read(&self, y: usize, xz: usize) -> u8 {
self.block.read().unwrap().bytes[y][xz]
}
pub fn write(&mut self, y: usize, xz: usize, value: u8) {
self.block.write().unwrap().bytes[y][xz] = value;
}
}
// =====================================================================
pub struct Stacks {
slots: [Bytes; constants::CHUNK_ALLOC_COUNT],
}
impl Stacks {
pub fn new() -> Stacks {
Stacks {
// Cannot Copy, I dont want a Copy I want to instance a fixed sized array of RwLocks<BU8> blocks
slots: [Bytes::new(); constants::CHUNK_ALLOC_COUNT],
}
}
}
}
fn main() {}
error[E0277]: the trait bound `example::Bytes: std::marker::Copy` is not satisfied
--> src/main.rs:53:24
|
53 | slots: [Bytes::new(); constants::CHUNK_ALLOC_COUNT],
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::marker::Copy` is not implemented for `example::Bytes`
|
= note: the `Copy` trait is required because the repeated element will be copied
The main thread spawns a child thread that will hold all game/core data on and will be accessible through each worker thread the child thread will spawn.
Each worker thread will be assigned workloads to read/write data on an assigned Block
on the Stacks
, but I don't know how to instance multiple RwLocks
this way or any other way without using the collections.
I tried adding #[derive(Copy, Clone)]
for each struct but the Bytes
struct error is:
error[E0204]: the trait `Copy` may not be implemented for this type
--> src/main.rs:24:18
|
24 | #[derive(Copy, Clone)]
| ^^^^
25 | pub struct Bytes {
26 | block: RwLock<BU8>,
| ------------------ this field does not implement `Copy`
I'm guessing RwLock
size cannot known at compile time so would a vector of pointers to each block of bytes work? If so how do I implement that safely?