error[E0277]: the trait bound `services::pool::zmq::Socket: std::clone::Clone` is not satisfied
--> src/services/pool/networker.rs:193:49
|
193 | let mut sockets: Vec<Option<ZSocket>> = vec![None; nodes.len()];
| ^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::clone::Clone` is not implemented for `services::pool::zmq::Socket`
|
= note: required because of the requirements on the impl of `std::clone::Clone` for `std::option::Option<services::pool::zmq::Socket>`
= note: required by `std::vec::from_elem`
and imaging that None should work because None is, kind of, the same for all types.
What is the best (for some definition of best) way to initialize such a vector with all None values?
Array initialization ([expr; count]) is a compiler builtin, on the other hand, vector initialization (vec![expr; count]) is fully a library construct. So please note we're talking about two separate constructs (although they share similar syntax for a reason).
vec![expr; count] must choose whether to:
evaluate expr once and then Clone it (this is how it's implemented), or
evaluate the expression count times. This doesn't require any bounds, and would work with None case.
Unfortunately, a macro has no power to distinguish whether expr has side effects and cannot support both 1. and 2. So if we don't want the user to be surprised that the expr is evaluated multiple times, we're stuck with 1.
Honestly, I'm not a fan of the vec macro whatsoever. I'd much prefer an API like below:
array is a block of memory on the stack that is described by a 2-component descriptor (usize, usize) consisting of non-zero start address and array length (in elements). vec is a block of memory in the heap that is described by a 3-component descriptor (usize, usize, usize) consisting of non-zero start address, length (in elements), and capacity (in elements).
Thanks. I thought that creating an array using the methods discussed in Josh's post and then turning it into a Vec would be clear code and not have too bad performance