pub trait Command: Send + 'static {
type Response: Send + 'static;
// Required methods
fn to_frame(&self) -> Frame;
fn parse_response(&self, frame: Frame) -> Result<Self::Response, RedisError>;
fn name(&self) -> &str;
// Provided methods
fn idempotent(&self) -> bool { ... }
fn is_blocking(&self) -> bool { ... }
fn deadline(&self) -> Option<Instant> { ... }
}Expand description
A typed Redis command.
Each Redis command is represented as a struct that implements this trait.
The associated Response type ensures compile-time type safety for
command results.
§Example
use redis_tower_core::{Command, Frame, RedisError};
use redis_tower_protocol::helpers::{array, bulk};
pub struct Ping;
impl Command for Ping {
type Response = String;
fn to_frame(&self) -> Frame {
array(vec![bulk("PING")])
}
fn parse_response(&self, frame: Frame) -> Result<String, RedisError> {
match frame {
Frame::SimpleString(s) => Ok(String::from_utf8_lossy(&s).into_owned()),
_ => Err(RedisError::UnexpectedResponse {
expected: "simple string",
actual: format!("{frame:?}"),
}),
}
}
fn name(&self) -> &str { "PING" }
}Required Associated Types§
Required Methods§
Sourcefn parse_response(&self, frame: Frame) -> Result<Self::Response, RedisError>
fn parse_response(&self, frame: Frame) -> Result<Self::Response, RedisError>
Parse a RESP response frame into the typed response.
Takes &self so that parsing can depend on command configuration
(e.g., optional flags that change the response shape).
Provided Methods§
Sourcefn idempotent(&self) -> bool
fn idempotent(&self) -> bool
Whether this command is safe to retry on connection errors.
Returns true for read-only commands (GET, HGET, LRANGE, etc.) and
commands where re-execution produces the same result (e.g. SET without
side-effect sub-commands). Returns false (the default) for all other
write commands where retrying may cause silent data duplication.
Override this method in command implementations to declare idempotency.
Sourcefn is_blocking(&self) -> bool
fn is_blocking(&self) -> bool
Whether this command blocks the connection until data is available or a
timeout elapses – BLPOP, BRPOP, BLMOVE, BZPOPMIN/BZPOPMAX, and
XREAD/XREADGROUP with BLOCK.
Returns false by default. This matters for multiplexed clients: a
blocking command holds the single shared pipeline worker for its entire
duration, stalling every other concurrent caller on that connection. Run
blocking commands on a dedicated RedisConnection or a pooled connection
instead. The flag lets callers and middleware detect such commands.
Sourcefn deadline(&self) -> Option<Instant>
fn deadline(&self) -> Option<Instant>
Return the absolute deadline attached to this command, if any.
Most commands have no deadline. WithDeadline overrides this method
so deadline-aware middleware and resource acquisition can share one
end-to-end budget instead of starting a fresh timeout at each stage.
Implementations that wrap another command should propagate the wrapped
command’s deadline.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".