RedisWrite in redis - Rust
Description: Abstraction trait for redis command abstractions.

Skip to main content

## RedisWrite

# Trait RedisWrite

Source

    pub trait RedisWrite {
        // Required methods
        fn write_arg(&mut self, arg: &[u8]);
        fn writer_for_next_arg(&mut self) -> impl Write + '_;

        // Provided methods
        fn write_arg_fmt(&mut self, arg: impl Display) { ... }
        fn reserve_space_for_args(
            &mut self,
            additional: impl IntoIterator<Item = usize>,
        ) { ... }
        fn bufmut_for_next_arg(&mut self, capacity: usize) -> impl BufMut + '_ { ... }
    }

Expand description

Abstraction trait for redis command abstractions.

## Required Methods§

Source

#### fn write_arg(&mut self, arg: &[u8])

Accepts a serialized redis command.

Source

#### fn writer_for_next_arg(&mut self) -> impl Write + '_

Appends an empty argument to the command, and returns a `std::io::Write` instance that can write to it.

Writing multiple arguments into this buffer is unsupported. The resulting data will be interpreted as one argument by Redis.

Writing no data is supported and is similar to having an empty bytestring as an argument.

## Provided Methods§

Source

#### fn write_arg_fmt(&mut self, arg: impl Display)

Accepts a serialized redis command.

Source

#### fn reserve_space_for_args( &mut self, additional: impl IntoIterator<Item = usize>, )

Reserve space for `additional` arguments in the command

`additional` is a list of the byte sizes of the arguments.

##### §Examples

Sending some Protobufs with `prost` to Redis.

ⓘ

    use prost::Message;

    let to_send: Vec<SomeType> = todo!();
    let mut cmd = Cmd::new();

    // Calculate and reserve the space for the args
    cmd.reserve_space_for_args(to_send.iter().map(Message::encoded_len));

    // Write the args to the buffer
    for arg in to_send {
        // Encode the type directly into the Cmd buffer
        // Supplying the required capacity again is not needed for Cmd,
        // but can be useful for other implementers like Vec<Vec<u8>>.
        arg.encode(cmd.bufmut_for_next_arg(arg.encoded_len()));
    }

##### §Implementation note

The default implementation provided by this trait is a no-op. It’s therefore strongly recommended to implement this function. Depending on the internal buffer it might only be possible to use the numbers of arguments (`additional.len()`) or the total expected capacity (`additional.iter().sum()`). Implementors should assume that the caller will be wrong and might over or under specify the amount of arguments and space required.

Source

#### fn bufmut_for_next_arg(&mut self, capacity: usize) -> impl BufMut + '_

Available on crate feature `bytes` only.

Appends an empty argument to the command, and returns a [`bytes::BufMut`] instance that can write to it.

`capacity` should be equal or greater to the amount of bytes expected, as some implementations might not be able to resize the returned buffer.

Writing multiple arguments into this buffer is unsupported. The resulting data will be interpreted as one argument by Redis.

Writing no data is supported and is similar to having an empty bytestring as an argument.

## Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

## Implementations on Foreign Types§

Source§

### impl RedisWrite for Vec<Vec<u8>>

Source§

#### fn write_arg(&mut self, arg: &[u8])

Source§

#### fn write_arg_fmt(&mut self, arg: impl Display)

Source§

#### fn writer_for_next_arg(&mut self) -> impl Write + '_

Source§

#### fn reserve_space_for_args( &mut self, additional: impl IntoIterator<Item = usize>, )

Source§

#### fn bufmut_for_next_arg(&mut self, capacity: usize) -> impl BufMut + '_

Available on crate feature `bytes` only.

## Implementors§

Source§

### impl RedisWrite for Cmd