Is it possible to create an Arc<Mutex<[T]>>
from an unknown-sized slice &[T]
?
Naively trying to implement arc_mutex_from_slice
as follows does not compile:
fn arc_mutex_from_slice<T>(v: &[T]) -> Arc<Mutex<[T]>> {
return Arc::new(Mutex::new(*v));
}
The compiler throws an error because the size of the slice is unknown at compile-time:
error[E0277]: the size for values of type `[T]` cannot be known at compilation time
--> src/main.rs:4:21
|
4 | return Arc::new(Mutex::new(*v));
| -------- ^^^^^^^^^^^^^^ doesn't have a size known at compile-time
| |
| required by a bound introduced by this call
|
= help: within `Mutex<[T]>`, the trait `Sized` is not implemented for `[T]`
= note: required because it appears within the type `Mutex<[T]>`
note: required by a bound in `Arc::<T>::new`
Is it possible to use unsafe
to create an Arc<Mutex<[T]>>
from an unknown-sized slice &[T]
?