Address feedback from Niklas

This commit is contained in:
James Wilson
2021-08-11 16:59:11 +01:00
parent f089ad1758
commit f26b39ac63
15 changed files with 62 additions and 38 deletions
+3 -2
View File
@@ -23,7 +23,8 @@ impl ByteSize {
pub fn new(bytes: usize) -> ByteSize {
ByteSize(bytes)
}
pub fn into_bytes(self) -> usize {
/// Return the number of bytes stored within.
pub fn num_bytes(self) -> usize {
self.0
}
}
@@ -101,7 +102,7 @@ mod test {
for (s, expected) in cases {
let b: ByteSize = s.parse().unwrap();
assert_eq!(b.into_bytes(), expected);
assert_eq!(b.num_bytes(), expected);
}
}
}
+23 -14
View File
@@ -11,7 +11,7 @@ pin_project! {
/// A simple enum that delegates implementation to one of
/// the two possible sinks contained within.
impl <A, B> EitherSink<A, B> {
impl<A, B> EitherSink<A, B> {
pub fn a(val: A) -> Self {
EitherSink::A { inner: val }
}
@@ -20,38 +20,47 @@ impl <A, B> EitherSink<A, B> {
}
}
impl <Item, Error, A, B> Sink<Item> for EitherSink<A, B>
impl<Item, Error, A, B> Sink<Item> for EitherSink<A, B>
where
A: Sink<Item, Error = Error>,
B: Sink<Item, Error = Error>
B: Sink<Item, Error = Error>,
{
type Error = Error;
fn poll_ready(self: std::pin::Pin<&mut Self>, cx: &mut std::task::Context<'_>) -> std::task::Poll<Result<(), Self::Error>> {
fn poll_ready(
self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Result<(), Self::Error>> {
match self.project() {
EitherSinkInner::A{ inner } => inner.poll_ready(cx),
EitherSinkInner::B{ inner } => inner.poll_ready(cx)
EitherSinkInner::A { inner } => inner.poll_ready(cx),
EitherSinkInner::B { inner } => inner.poll_ready(cx),
}
}
fn start_send(self: std::pin::Pin<&mut Self>, item: Item) -> Result<(), Self::Error> {
match self.project() {
EitherSinkInner::A{ inner } => inner.start_send(item),
EitherSinkInner::B{ inner } => inner.start_send(item)
EitherSinkInner::A { inner } => inner.start_send(item),
EitherSinkInner::B { inner } => inner.start_send(item),
}
}
fn poll_flush(self: std::pin::Pin<&mut Self>, cx: &mut std::task::Context<'_>) -> std::task::Poll<Result<(), Self::Error>> {
fn poll_flush(
self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Result<(), Self::Error>> {
match self.project() {
EitherSinkInner::A{ inner } => inner.poll_flush(cx),
EitherSinkInner::B{ inner } => inner.poll_flush(cx)
EitherSinkInner::A { inner } => inner.poll_flush(cx),
EitherSinkInner::B { inner } => inner.poll_flush(cx),
}
}
fn poll_close(self: std::pin::Pin<&mut Self>, cx: &mut std::task::Context<'_>) -> std::task::Poll<Result<(), Self::Error>> {
fn poll_close(
self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Result<(), Self::Error>> {
match self.project() {
EitherSinkInner::A{ inner } => inner.poll_close(cx),
EitherSinkInner::B{ inner } => inner.poll_close(cx)
EitherSinkInner::A { inner } => inner.poll_close(cx),
EitherSinkInner::B { inner } => inner.poll_close(cx),
}
}
}
+1 -1
View File
@@ -153,7 +153,7 @@ fn header_contains_value(
pub fn trim(x: &[u8]) -> &[u8] {
let from = match x.iter().position(|x| !x.is_ascii_whitespace()) {
Some(i) => i,
None => return &x[0..0],
None => return &[],
};
let to = x.iter().rposition(|x| !x.is_ascii_whitespace()).unwrap();
&x[from..=to]
+2 -2
View File
@@ -27,15 +27,15 @@ pub mod ws_client;
mod assign_id;
mod dense_map;
mod either_sink;
mod mean_list;
mod most_seen;
mod num_stats;
mod either_sink;
// Export a bunch of common bits at the top level for ease of import:
pub use assign_id::AssignId;
pub use dense_map::DenseMap;
pub use either_sink::EitherSink;
pub use mean_list::MeanList;
pub use most_seen::MostSeen;
pub use num_stats::NumStats;
pub use either_sink::EitherSink;
+5
View File
@@ -28,6 +28,8 @@ pub struct MostSeen<T> {
impl<T: Default> Default for MostSeen<T> {
fn default() -> Self {
// This sets the "most seen item" to the default value for the type,
// and notes that nobody has actually seen it yet (current_count is 0).
Self {
current_best: T::default(),
current_count: 0,
@@ -38,6 +40,9 @@ impl<T: Default> Default for MostSeen<T> {
impl<T> MostSeen<T> {
pub fn new(item: T) -> Self {
// This starts us off with an item that we've seen. This item is set as
// the "most seen item" and the current_count is set to 1, as we've seen it
// once by virtue of providing it here.
Self {
current_best: item,
current_count: 1,
+16 -7
View File
@@ -26,7 +26,7 @@ pub type BlockNumber = u64;
pub type Timestamp = u64;
pub use primitive_types::H256 as BlockHash;
///
/// Basic node details.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct NodeDetails {
pub chain: Box<str>,
@@ -38,13 +38,22 @@ pub struct NodeDetails {
pub startup_time: Option<Box<str>>,
}
///
/// A couple of node statistics.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct NodeStats {
pub peers: u64,
pub txcount: u64,
}
// # A note about serialization/deserialization of types in this file:
//
// Some of the types here are sent to UI feeds. In an effort to keep the
// amount of bytes sent to a minimum, we have written custom serializers
// for those types.
//
// For testing purposes, it's useful to be able to deserialize from some
// of these types so that we can test message feed things, so custom
// deserializers exist to undo the work of the custom serializers.
impl Serialize for NodeStats {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
@@ -67,7 +76,7 @@ impl<'de> Deserialize<'de> for NodeStats {
}
}
///
/// Node IO details.
#[derive(Default)]
pub struct NodeIO {
pub used_state_cache_size: MeanList<f32>,
@@ -85,7 +94,7 @@ impl Serialize for NodeIO {
}
}
///
/// Concise block details
#[derive(Deserialize, Serialize, Debug, Clone, Copy, PartialEq)]
pub struct Block {
pub hash: BlockHash,
@@ -101,7 +110,7 @@ impl Block {
}
}
///
/// Node hardware details.
#[derive(Default)]
pub struct NodeHardware {
/// Upload uses means
@@ -126,7 +135,7 @@ impl Serialize for NodeHardware {
}
}
///
/// Node location details
#[derive(Debug, Clone, PartialEq)]
pub struct NodeLocation {
pub latitude: f32,
@@ -161,7 +170,7 @@ impl<'de> Deserialize<'de> for NodeLocation {
}
}
///
/// Verbose block details
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct BlockDetails {
pub block: Block,
+2 -2
View File
@@ -72,7 +72,7 @@ impl<St: Stream> Stream for ReadyChunksAll<St> {
return if this.items.is_empty() {
Poll::Pending
} else {
Poll::Ready(Some(mem::replace(this.items, Vec::new())))
Poll::Ready(Some(mem::take(this.items)))
}
}
@@ -87,7 +87,7 @@ impl<St: Stream> Stream for ReadyChunksAll<St> {
let last = if this.items.is_empty() {
None
} else {
let full_buf = mem::replace(this.items, Vec::new());
let full_buf = mem::take(this.items);
Some(full_buf)
};
+2 -2
View File
@@ -36,8 +36,8 @@ pub type RawReceiver =
/// A websocket connection. From this, we can either expose the raw connection
/// or expose a cancel-safe interface to it.
pub struct Connection {
tx: soketto::connection::Sender<tokio_util::compat::Compat<tokio::net::TcpStream>>,
rx: soketto::connection::Receiver<tokio_util::compat::Compat<tokio::net::TcpStream>>,
tx: RawSender,
rx: RawReceiver,
}
impl Connection {