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(false));
let amt = encode(&mut buf, &frame, false).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, false).expect("Failed to encode frame");
socket.write_all(&buf).await.expect("Failed to write to socket");
let _ = buf.split_to(amt);
}
Functions§
- encode
- Attempt to encode a frame into
buf
. - encode_
borrowed - Attempt to encode a borrowed frame into
buf
. - encode_
bytes bytes
- Attempt to encode a frame into
buf
. - extend_
encode bytes
- Attempt to encode a frame at the end of
buf
, extending the buffer before encoding. - extend_
encode_ borrowed bytes
- Attempt to encode a borrowed frame at the end of
buf
, extending the buffer before encoding.