-
Notifications
You must be signed in to change notification settings - Fork 169
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
additional useful derives #1235
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -264,7 +264,7 @@ pub use status::StatusCode; | |
|
||
/// Information sent by the server back to this client | ||
/// during initial connection, and possibly again later. | ||
#[derive(Debug, Deserialize, Default, Clone, Eq, PartialEq)] | ||
#[derive(Debug, Deserialize, Serialize, Default, Clone, Eq, PartialEq)] | ||
pub struct ServerInfo { | ||
/// The unique identifier of the NATS server. | ||
#[serde(default)] | ||
|
@@ -1293,7 +1293,7 @@ impl std::fmt::Display for ServerError { | |
} | ||
|
||
/// Info to construct a CONNECT message. | ||
#[derive(Clone, Debug, Serialize)] | ||
#[derive(Clone, Debug, PartialEq, Eq, Hash, Deserialize, Serialize)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think there is any benefit in addint any addional traits to ConnectInfo. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't need this quite yet, but was intending on caching them on the server per-connection, and will be deserializing them on the server. |
||
pub struct ConnectInfo { | ||
/// Turns on +OK protocol acknowledgments. | ||
pub verbose: bool, | ||
|
@@ -1354,7 +1354,7 @@ pub struct ConnectInfo { | |
} | ||
|
||
/// Protocol version used by the client. | ||
#[derive(Serialize_repr, Deserialize_repr, PartialEq, Eq, Debug, Clone, Copy)] | ||
#[derive(Serialize_repr, Deserialize_repr, PartialEq, Eq, Hash, Debug, Clone, Copy)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the use case for adding hash here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In order to store the |
||
#[repr(u8)] | ||
pub enum Protocol { | ||
/// Original protocol. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,10 +2,10 @@ use bytes::Bytes; | |
use serde::{Deserialize, Serialize}; | ||
use std::fmt; | ||
use std::ops::Deref; | ||
use std::str::{from_utf8, Utf8Error}; | ||
use std::str::{from_utf8, FromStr, Utf8Error}; | ||
|
||
/// A `Subject` is an immutable string type that guarantees valid UTF-8 contents. | ||
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] | ||
#[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This one indeed could be useful. |
||
pub struct Subject { | ||
bytes: Bytes, | ||
} | ||
|
@@ -105,6 +105,14 @@ impl From<String> for Subject { | |
} | ||
} | ||
|
||
impl FromStr for Subject { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no need for this trait as we have impl From<String> for Subject {
fn from(s: String) -> Self {
// Since the input `String` is guaranteed to be valid UTF-8, we can
// safely transmute the internal Vec<u8> to a Bytes value.
let bytes = Bytes::from(s.into_bytes());
Subject { bytes }
}
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Jarema I included this to round out the stdlib traits - where I'm using a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From and FromStr are not the same. FromStr is from a ro slice of memory and There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't claim they were the same, only that one ( |
||
type Err = Utf8Error; | ||
|
||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
Ok(Self::from(s)) | ||
} | ||
} | ||
|
||
impl AsRef<str> for Subject { | ||
fn as_ref(&self) -> &str { | ||
self | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ServerInfo is never serialized.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the server I'm writing, I'm sending it back to the client :).