How do you properly implement the required methods of a foreign trait in Rust?

I need to implement the ConnectionLike trait from Redis for my own wrapper struct to bypass the orphan rule:

pub trait ConnectionLike {
    // Required methods
    fn req_packed_command(&mut self, cmd: &[u8]) -> RedisResult<Value>;
    fn req_packed_commands(
        &mut self,
        cmd: &[u8],
        offset: usize,
        count: usize
    ) -> RedisResult<Vec<Value>>;
    fn get_db(&self) -> i64;
    fn check_connection(&mut self) -> bool;
    fn is_open(&self) -> bool;

    // Provided method
    fn req_command(&mut self, cmd: &Cmd) -> RedisResult<Value> { ... }
}

URL: ConnectionLike in redis - Rust

How do I implement the logic of each required method? Should I try to dig up Redis's own implementation of ConnectionLike within their crate and copy their code?

Certainly not. If your wrapper is indeed a wrapper, then you probably have another ConnectionLike in it, in which case you should just forward the methods to it.

3 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.