More of a design question. I want to record Latency and Packet Loss Rate on
SocketAddr. I want to implement it as a trait, as Latency and Packet Loss is a "trait" of
PingStats trait exposes two methods:
get_mean_latency(&self) -> Option<Duration> and
get_packet_loss_percentage(&self) -> Option<f32>. These two methods need to share variables like a struct, i.e.
last_ten_stun_rtt: [Duration; 10]. AFAIK storing mutable variables in traits is impossible.
How do I approach this?
Don't make it a trait unless you have a reason to do so, such as functions or types that need to work with multiple different types that implement that functionality. As you noted you need to keep track of some extra data to implement the functionality you want, and you can't store that inside SocketAddr, so you shouldn't implement that functionality directly on SocketAddr.
I would create a new struct that has the inner SocketAddr and the extra array as fields, and implement whatever functionality you need as methods/associated functions for that type.