Skip to content

Commit

Permalink
remove lifetimes
Browse files Browse the repository at this point in the history
  • Loading branch information
wuan committed Jan 4, 2023
1 parent dbb63d1 commit 2afcdda
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 41 deletions.
29 changes: 9 additions & 20 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,26 +477,15 @@ impl<S: Into<String>> From<S> for EitherSide {
}

#[derive(PartialEq, Eq, Clone, Debug, Deserialize, Serialize, derive_more::Display)]
#[display(fmt = "{}-{}", "&*_0[0]", "&*_0[1]")]
pub struct ClientVersion<'a>(#[serde(borrow)] pub [&'a str; 2]);

impl<'a> ClientVersion<'a> {
pub fn new(name: &'a str, version: &'a str) -> Self {
let array = [name, version];
ClientVersion { 0: array }
}
}

impl<'a> std::ops::Deref for ClientVersion<'a> {
type Target = [&'a str; 2];
fn deref(&self) -> &[&'a str; 2] {
&self.0
}
}

impl<'a> std::ops::DerefMut for ClientVersion<'a> {
fn deref_mut(&mut self) -> &mut [&'a str; 2] {
&mut self.0
#[display(fmt = "{}-{}", "&*_0", "&*_1")]
pub struct ClientVersion(pub Cow<'static, str>, pub Cow<'static, str>);

impl ClientVersion {
pub fn new(implementation: Cow<'static, str>, version: Cow<'static, str>) -> Self {
ClientVersion {
0: implementation,
1: version,
}
}
}

Expand Down
9 changes: 3 additions & 6 deletions src/core/rendezvous.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ struct WsConnection {
}

impl WsConnection {
async fn send_message<'a>(
async fn send_message(
&mut self,
message: &OutboundMessage<'a>,
message: &OutboundMessage,
queue: Option<&mut MessageQueue>,
) -> Result<(), RendezvousError> {
log::debug!("Sending {}", message);
Expand Down Expand Up @@ -322,10 +322,7 @@ impl RendezvousServer {
&self.side
}

async fn send_message<'a>(
&mut self,
message: &OutboundMessage<'a>,
) -> Result<(), RendezvousError> {
async fn send_message(&mut self, message: &OutboundMessage) -> Result<(), RendezvousError> {
self.connection
.send_message(message, self.state.as_mut().map(|state| &mut state.queue))
.await
Expand Down
26 changes: 11 additions & 15 deletions src/core/server_messages.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{AppID, ClientVersion, Mailbox, Mood, MySide, Nameplate, Phase, TheirSide};
use serde_derive::{Deserialize, Serialize};
use std::collections::HashMap;
use std::{borrow::Cow, collections::HashMap};

/// Special encoding for the `nameplates` message
#[derive(Serialize, Deserialize, Debug, PartialEq)]
Expand Down Expand Up @@ -132,7 +132,7 @@ impl EncryptedMessage {
#[derive(Serialize, Debug, PartialEq, derive_more::Display)]
#[serde(rename_all = "kebab-case")]
#[serde(tag = "type")]
pub enum OutboundMessage<'a> {
pub enum OutboundMessage {
#[display(fmt = "SubmitPermission({})", _0)]
SubmitPermission(SubmitPermission),
#[display(
Expand All @@ -144,7 +144,7 @@ pub enum OutboundMessage<'a> {
Bind {
appid: AppID,
side: MySide,
client_version: ClientVersion<'a>,
client_version: ClientVersion,
},
List,
Allocate,
Expand Down Expand Up @@ -182,10 +182,14 @@ pub enum OutboundMessage<'a> {
}

const CLIENT_NAME: &str = "rust";
impl<'a> OutboundMessage<'a> {

impl OutboundMessage {
pub fn bind(appid: AppID, side: MySide) -> Self {
let client_version_string: &str = env!("CARGO_PKG_VERSION");
let client_version = ClientVersion::new(CLIENT_NAME, client_version_string);
let client_version = ClientVersion::new(
Cow::Borrowed(CLIENT_NAME),
Cow::Borrowed(client_version_string),
);
OutboundMessage::Bind {
appid,
side,
Expand Down Expand Up @@ -267,11 +271,10 @@ pub enum InboundMessage {
mod test {
use super::*;
use serde_json::{from_str, json, Value};
use std::ops::Deref;

#[test]
fn test_bind() {
let client_version_string: String = String::from(env!("CARGO_PKG_VERSION"));
let client_version_string: &str = env!("CARGO_PKG_VERSION");
let m1 = OutboundMessage::bind(
AppID::new("appid"),
MySide::unchecked_from_string(String::from("side1")),
Expand All @@ -287,18 +290,11 @@ mod test {

#[test]
fn test_client_version_string_rep() {
let client_version = ClientVersion::new("foo", "1.0.2");
let client_version = ClientVersion::new(Cow::Borrowed("foo"), Cow::Borrowed("1.0.2"));

assert_eq!(client_version.to_string(), "foo-1.0.2")
}

#[test]
fn test_client_version_deref() {
let client_version = ClientVersion::new("bar", "0.8.9");

assert_eq!(client_version.deref(), &["bar", "0.8.9"])
}

#[test]
fn test_list() {
let m1 = OutboundMessage::List;
Expand Down

0 comments on commit 2afcdda

Please sign in to comment.