#[derive(Debug,Serialize,Deserialize)]
struct ApiPiece<O : ApiPieceOp> {
- ctoken : String,
+ ctoken : RawToken,
piece : VisiblePieceId,
gen : Generation,
cseq : ClientSequence,
fn api_piece_op<O: ApiPieceOp>(form : Json<ApiPiece<O>>)
-> 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();
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)]
#[derive(Clone,Debug)]
pub struct InstanceAccess<'i, Id> {
- pub raw_token : &'i str,
+ pub raw_token : &'i RawTokenVal,
pub i : InstanceAccessDetails<Id>,
}
// ========== implementations ==========
+/*
impl Borrow<str> 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<RawTokenVal> 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 ----------
}
}
-pub fn lookup_token<Id : AccessId>(s : &str)
+pub fn lookup_token<Id : AccessId>(s : &RawTokenVal)
-> Result<InstanceAccessDetails<Id>, OE> {
Id::global_tokens(PRIVATE_Y).read().unwrap().get(s).cloned()
.ok_or(Id::ERROR)
#[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() }
}
#[derive(Deserialize)]
struct SessionForm {
- ptoken : String,
+ ptoken : RawToken,
}
#[post("/_/session", format="json", data="<form>")]
fn session(form : Json<SessionForm>) -> Result<Template,OE> {
// 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()?;
//---------- 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)
}
}