Last active
October 19, 2021 18:14
-
-
Save Voronar/423dec7b006ca14defaca4ee9e2b7798 to your computer and use it in GitHub Desktop.
Rust notes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[derive(Debug)] | |
pub struct Ar(Enum); | |
impl Serialize for Ar { | |
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> | |
where | |
S: serde::Serializer, | |
{ | |
serializer.serialize_newtype_struct("Ar", &self.0) | |
} | |
} | |
impl<'de> Deserialize<'de> for Ar { | |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | |
where | |
D: serde::Deserializer<'de>, | |
{ | |
Ok(Ar(Enum::deserialize(deserializer)?)) | |
} | |
} | |
#[derive(Serialize, Deserialize)] | |
pub struct Typ { | |
ar: Ar, | |
} | |
#[test] | |
fn tt() { | |
println!("{:#?}", serde_json::from_str::<Ar>("\"En1\"")); | |
println!("{}", serde_json::to_string(&Ar(Enum::En1)).unwrap()); | |
} | |
#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, Display)] | |
pub enum Enum { | |
En1, | |
En2, | |
En3, | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
impl Frame { | |
/// Fragmentaion logic for two side frames | |
pub fn two_frame_fragmentaion(first: &mut Frame, second: &mut Frame, first_opdata: OpCode) { | |
let fh = first.header_mut(); | |
fh.is_final = false; | |
fh.opcode = first_opdata; | |
let sh = second.header_mut(); | |
sh.is_final = true; | |
sh.opcode = OpCode::Data(Data::Continue); | |
} | |
} | |
fn write_message() { | |
let frames = match message { | |
Message::Ping(data) => vec![Frame::ping(data)], | |
Message::Pong(data) => { | |
self.pong = Some(Frame::pong(data)); | |
return self.write_pending(stream); | |
} | |
Message::Close(code) => return self.close(stream, code), | |
// text or binary | |
_ => { | |
let (mut data, opdata) = match message { | |
Message::Text(d) => (d.into(), OpData::Text), | |
Message::Binary(d) => (d, OpData::Binary), | |
_ => return self.write_pending(stream), | |
}; | |
let mut frames = vec![]; | |
while data.len() > 0 { | |
let res: Vec<_> = data.drain(..data.len().min(4096)).collect(); | |
let frame = Frame::message(res, OpCode::Data(OpData::Continue), false); | |
frames.push(frame); | |
} | |
match frames.as_mut_slice() { | |
[] => {} | |
[first] => { | |
let fh = first.header_mut(); | |
fh.is_final = true; | |
fh.opcode = OpCode::Data(opdata); | |
} | |
[first, second] => { | |
Frame::two_frame_fragmentaion(first, second, OpCode::Data(opdata)); | |
} | |
[first, .., last] => { | |
Frame::two_frame_fragmentaion(first, last, OpCode::Data(opdata)); | |
} | |
}; | |
frames | |
} | |
}; | |
self.send_queue.extend(frames); | |
self.write_pending(stream) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment