static X: usize = 0
would be forbidden, while static X: AtomicUsize = ATOMIC_USIZE_INIT
is perfectly fine.
trait AsOption<'a>: Sync
Menu
says that it can contain any kind of AsOption
AsOption
might not be threadsafe (for example, an AsOption
might contain Cell<i32>
inside).
: Sync
you oblige all implementors of AsOption
to gurantee thread safety.
mut
data in rust is not actually guranteed to be immutable.
mut
reference.
&mut
would be "unique reference", and for &
"potentially shared reference".
Immutable
trait bound like Sync
, but then you'll have to add AsOption: Immutable
anyway, to specify that an immutable menu can hold only immutable options.
AtomicUsizes
to count calls for cheap profiling for example :)
State::Failed(ref msg)
, which is not what I want to
match self { A {..} => method_a(self) }
?
ET::EV { ref val1, ref va2 }
and not use say val1, you get warning which suggests you to replace it with _val1, which is not working for obvious reasons
ET::EV { ref va2, .. }
should work then
fn to_bytes<'a>(buf: (&'a mut [u8], usize), packet: Packet) -> Result<(&'a mut [u8], usize), GenError> {
match packet {
Packet::RouteRequest {..} => to_bytes_route_request(buf, packet),
Packet::RouteResponse {..} => to_bytes_route_response(buf, packet),
Packet::ConnectNotification {..} => to_bytes_connect_notification(buf, packet),
Packet::DisconnectNotification {..} => to_bytes_disconnect_notification(buf, packet),
Packet::PingRequest {..} => to_bytes_ping_request(buf, packet),
Packet::PongResponse {..} => to_bytes_pong_response(buf, packet),
Packet::OobSend {..} => to_bytes_oob_send(buf, packet),
Packet::OobReceive {..} => to_bytes_oob_recv(buf, packet),
Packet::Data {..} => to_bytes_data(buf, packet)
}
}
fn to_bytes_route_request<'a>(buf: (&'a mut [u8], usize), packet: Packet) -> Result<(&'a mut [u8], usize), GenError> {
match packet {
Packet::RouteRequest { peer_pk } => {
do_gen!(buf,
gen_be_u8!(0x00) >>
gen_slice!(peer_pk.as_ref())
)
},
_ => Err(GenError::NotYetImplemented)
}
}
it's just misleading warning
yep :( It should suggest at least something like val1: ref _val1
(though it looks much uglier then just ..
)
But as I was said
so you can't define method which works on particular variant by type
struct Packet { tpe: PacketType, payload: Vec<u8/whatever> }
struct Packet { tpe: PacketType, payload: Vec<u8/whatever> }
wont help
to_bytes_...
?
from_bytes
-> &PacketTrait
let x: X = X::from_bytes(bytes)
Packet::from_bytes
in this case?
fn to_bytes<'a>(buf: (&'a mut [u8], usize), packet: Packet) -> Result<(&'a mut [u8], usize), GenError> {
match packet {
Packet::RouteRequest {..} => to_bytes_route_request(buf, packet),
Packet::RouteResponse {..} => to_bytes_route_response(buf, packet),
Packet::ConnectNotification {..} => to_bytes_connect_notification(buf, packet),
Packet::DisconnectNotification {..} => to_bytes_disconnect_notification(buf, packet),
Packet::PingRequest {..} => to_bytes_ping_request(buf, packet),
Packet::PongResponse {..} => to_bytes_pong_response(buf, packet),
Packet::OobSend {..} => to_bytes_oob_send(buf, packet),
Packet::OobReceive {..} => to_bytes_oob_recv(buf, packet),
Packet::Data {..} => to_bytes_data(buf, packet)
}
}
struct PingRequest { ... }
enum Request {
PingRequest(PingRequest),
...
}
enum Packet {
Packet1(bytes: &[u8]),
Packet1(bytes: &[u8])
}
seems like it's fine when I know the type of what I received in bytes
like let x: X = X::from_bytes(bytes)
but what would it be for a generic Packet::from_bytes in this case?
enum PacketType { Packet1, Packet2 } struct Packet<'a> { packet_type: PacketType, bytes: &'a [u8] }
Packet::from_bytes
?
Packet
enum
of all packet types you can parse. Each variant of this enum contains a struct
with the packet fields.
buf.extend_from_slice(&stack_buf);
did you mean &stack_buf[..produced]
?
assignment to immutably borrowed _mutable_integer occurs here
instead of assignment to borrowed _mutable_integer occurs here
?