From 97039552f9dcaf45e9d8b3d163c0ac235cf5269e Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sat, 5 Sep 2020 23:53:03 +0100 Subject: [PATCH] newtype RawTokenVal --- src/api.rs | 4 ++-- src/bin/server.rs | 2 +- src/global.rs | 28 +++++++++++++++++++++++++--- src/session.rs | 4 ++-- src/spec.rs | 12 ++++++++---- 5 files changed, 38 insertions(+), 12 deletions(-) diff --git a/src/api.rs b/src/api.rs index b0da887a..75e8f0ef 100644 --- a/src/api.rs +++ b/src/api.rs @@ -4,7 +4,7 @@ use crate::http::*; #[derive(Debug,Serialize,Deserialize)] struct ApiPiece { - ctoken : String, + ctoken : RawToken, piece : VisiblePieceId, gen : Generation, cseq : ClientSequence, @@ -66,7 +66,7 @@ impl Lens for TransparentLens { fn api_piece_op(form : Json>) -> impl response::Responder<'static> { // thread::sleep(Duration::from_millis(2000)); - let iad = lookup_token(&form.ctoken)?; + let iad = lookup_token(form.ctoken.borrow())?; let client = iad.ident; let mut ig = iad.gref.lock()?; ig.save_game_later(); diff --git a/src/bin/server.rs b/src/bin/server.rs index cb089fa4..2de288e2 100644 --- a/src/bin/server.rs +++ b/src/bin/server.rs @@ -37,7 +37,7 @@ impl<'r> FromParam<'r> for CheckedResourceLeaf { #[derive(Serialize,Debug)] struct LoadingRenderContext<'r> { - ptoken : &'r str, + ptoken : &'r RawTokenVal, } #[get("/")] #[throws(OE)] diff --git a/src/global.rs b/src/global.rs index ca143c26..3b933e81 100644 --- a/src/global.rs +++ b/src/global.rs @@ -12,6 +12,10 @@ const MAX_CLIENT_INACTIVITY : Duration = Duration::from_secs(200); const GAME_SAVE_LAG : Duration = Duration::from_millis(500); +#[derive(Hash,Ord,PartialOrd,Eq,PartialEq,Serialize)] +#[repr(transparent)] +pub struct RawTokenVal(str); + // ---------- public data structure ---------- #[derive(Debug,Serialize,Deserialize)] @@ -137,7 +141,7 @@ pub struct InstanceAccessDetails { #[derive(Clone,Debug)] pub struct InstanceAccess<'i, Id> { - pub raw_token : &'i str, + pub raw_token : &'i RawTokenVal, pub i : InstanceAccessDetails, } @@ -183,8 +187,26 @@ const PRIVATE_Y : PrivateCaller = PrivateCaller(()); // ========== implementations ========== +/* impl Borrow for RawToken { fn borrow(&self) -> &str { &self.0 } +}*/ + +impl RawTokenVal { + // str is [u8] with a funny hat on, so &str is pointer + byte count. + // nomicon says &SomeStruct([T]) is pointer plus number of elements. + // So &str and &SomeStruct(str) have the same layout + fn from_str(s: &str) -> &RawTokenVal { unsafe { mem::transmute(s) } } +} + +impl Borrow for RawToken { + fn borrow(&self) -> &RawTokenVal { RawTokenVal::from_str(&self.0) } +} + +impl Debug for RawTokenVal { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + crate::spec::implementation::raw_token_debug_as_str(&self.0, f) + } } // ---------- Main API for instance lifecycle ---------- @@ -789,7 +811,7 @@ impl RawToken { } } -pub fn lookup_token(s : &str) +pub fn lookup_token(s : &RawTokenVal) -> Result, OE> { Id::global_tokens(PRIVATE_Y).read().unwrap().get(s).cloned() .ok_or(Id::ERROR) @@ -802,7 +824,7 @@ impl<'r, Id> FromParam<'r> for InstanceAccess<'r, Id> #[throws(OE)] fn from_param(param: &'r RawStr) -> Self { let g = Id::global_tokens(PRIVATE_Y).read().unwrap(); - let token = param.as_str(); + let token = RawTokenVal::from_str(param.as_str()); let i = g.get(token).ok_or(Id::ERROR)?; InstanceAccess { raw_token : token, i : i.clone() } } diff --git a/src/session.rs b/src/session.rs index a65924c8..c13d75f9 100644 --- a/src/session.rs +++ b/src/session.rs @@ -40,12 +40,12 @@ struct DataLoadPlayer { #[derive(Deserialize)] struct SessionForm { - ptoken : String, + ptoken : RawToken, } #[post("/_/session", format="json", data="
")] fn session(form : Json) -> Result { // make session in this game, log a message to other players - let iad = lookup_token(&form.ptoken)?; + let iad = lookup_token(form.ptoken.borrow())?; let player = iad.ident; let c = { let mut ig = iad.gref.lock()?; diff --git a/src/spec.rs b/src/spec.rs index 26943187..d8a72ced 100644 --- a/src/spec.rs +++ b/src/spec.rs @@ -99,16 +99,20 @@ pub mod piece_specs { //---------- Implementation ---------- -mod implementation { +pub mod implementation { use super::*; use crate::imports::*; type Insn = crate::commands::MgmtGameInstruction; + pub fn raw_token_debug_as_str(s: &str, f: &mut fmt::Formatter) + -> fmt::Result { + let len = min(5, s.len() / 2); + write!(f, "{:?}...", &s[0..len]) + } + impl Debug for RawToken { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - let s = &self.0; - let len = min(5, s.len() / 2); - write!(f, "{:?}...", &s[0..len]) + raw_token_debug_as_str(&self.0, f) } } -- 2.30.2