use bytes::BytesMut;usetokio::net::TcpStream; pub struct Connection { stream: TcpStream, buffer: BytesMut, } impl Connection { pub fn new(stream: TcpStream) -> Connection { Connection { stream, // Allocate the buffer with 4kb of capacity. buffer: BytesMut::with_capacity(4096), } } ...
fn into_response(self) -> Response { // Use a smallinitial capacityof 128 bytes like serde_json::to_vec // https://docs.rs/serde_json/1.0.82/src/serde_json/ser.rs.html#2189 let mut buf = BytesMut::with_capacity(128).writer(); match serde_json::to_writer(&mut buf, &self....
let mut buf = BytesMut::with_capacity(64); buf.put(&b"hello server 1"[..]); conn.send_raw(b"hello server 1".to_vec()).await.unwrap(); let resp_bytes = conn.next().await.unwrap(); println!("[client] [4] receive: {:?}", resp_bytes); if let Some(recv_bytes) = conn....
fn poll(&mutself) -> Poll<Item, io::Error> {let mut buf = [0;1024];let mut bytes = bytesMut::new;loop {matchself.socket.read(&mut buf) {Async::Ready(0) =>returnAsync::Ready(bytes.to_vec),Async::Ready(n) => bytes.put(buf[0..n]),Async::NotReady =>returnAsync::NotReady,...
dst.extend(item.as_bytes()); Ok(()) } } impl Decoder for PisaProxy { type Item = Message; type Error = ProtocolError; fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> { if src.is_empty() { ...
use bytes::{Bytes, BytesMut}; use derive_more::{Display, Error}; use futures_core::{ready, Stream}; use futures_util::stream::once; use rustls::{Certificate, PrivateKey, ServerConfig as RustlsServerConfig, ServerName}; use rustls::{pki_types::ServerName, ServerConfig as RustlsServer...
fn encode(&mut self, item: Message, dst: &mut BytesMut) ->Result<(), Self::Error> { dst.extend(item.as_bytes()); Ok(()) } } impl Decoder for PisaProxy { type Item = Message; type Error = ProtocolError; fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Ite...
to_string(), )), } } } #[cfg(test)] mod tests { use bytes::BytesMut; use crate::RespDecode; use super::*; #[test] fn test_sadd_from_resp_array() -> anyhow::Result<()> { let mut buf = BytesMut::new(); buf.extend_from_slice(b"*4\r\n$4\r\nSADD\r\n$4\r\nlilp...
let mut bytes = bytesMut::new(); loop { match self.socket.read(&mut buf) { Async::Ready(0) => return Async::Ready(bytes.to_vec()), Async::Ready(n) => bytes.put(buf[0..n]), Async::NotReady => return Async::NotReady, ...
在这里,Encoder 编码是指将用户自定义类型转换成 BytesMut 类型,写到 TcpStream 中,Decoder 解码指将读到的字节数据序列化为 Rust 的结构体。 implEncoder<Message>forPisaProxy{typeError= ProtocolError;fnencode(&mutself, item: Message, dst: &mutBytesMut)->Result<(),Self::Error> { ...