Module redis_protocol::resp3::encode::complete
source · Available on crate feature
resp3 only.Expand description
Encoding functions for complete frames.
§Examples
§Using owned types:
use std::net::TcpStream;
fn example(socket: &mut TcpStream) {
// in many cases the starting buffer won't be empty, so this example shows how to track the offset as well
let frame = OwnedFrame::Array {
// send `HGET foo bar`
data: vec![
OwnedFrame::BlobString { data: "HGET".into(), attributes: None },
OwnedFrame::BlobString { data: "foo".into(), attributes: None },
OwnedFrame::BlobString { data: "bar".into(), attributes: None },
],
attributes: None
};
let mut buf = Vec::with_capacity(frame.encode_len());
let amt = encode(&mut buf, &frame).expect("Failed to encode frame");
debug_assert_eq!(buf.len(), amt);
socket.write_all(&buf).expect("Failed to write to socket");
}§Using bytes types with Tokio:
use tokio::net::TcpStream;
async fn example(socket: &mut TcpStream, buf: &mut BytesMut) {
// in many cases the starting buffer won't be empty, so this example shows how to track the offset as well
let frame = BytesFrame::Array {
// send `HGET foo bar`
data: vec![
BytesFrame::BlobString { data: "HGET".into(), attributes: None },
BytesFrame::BlobString { data: "foo".into(), attributes: None },
BytesFrame::BlobString { data: "bar".into(), attributes: None },
],
attributes: None
};
let amt = extend_encode(buf, &frame).expect("Failed to encode frame");
socket.write_all(&buf).await.expect("Failed to write to socket");
let _ = buf.split_to(amt);
}Functions§
- Attempt to encode a frame into
buf. - encode_bytes
bytesAttempt to encode a frame intobuf. - extend_encode
bytesAttempt to encode a frame at the end ofbuf, extending the buffer before encoding.