Skip to content

Commit

Permalink
Pipeline & Transaction don't clone anymore Client
Browse files Browse the repository at this point in the history
- fix doc tests
  • Loading branch information
mcatanzariti committed Mar 7, 2023
1 parent 020a376 commit 5946ba2
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 85 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ use rustis::{
#[tokio::main]
async fn main() -> Result<()> {
// Connect the client to a Redis server from its IP and port
let mut client = Client::connect("127.0.0.1:6379").await?;
let client = Client::connect("127.0.0.1:6379").await?;

// Flush all existing data in Redis
client.flushdb(FlushingMode::Sync).await?;
Expand Down
6 changes: 3 additions & 3 deletions src/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ impl Client {
/// #[cfg_attr(feature = "tokio-runtime", tokio::main)]
/// #[cfg_attr(feature = "async-std-runtime", async_std::main)]
/// async fn main() -> Result<()> {
/// let mut client = Client::connect("127.0.0.1:6379").await?;
/// let client = Client::connect("127.0.0.1:6379").await?;
///
/// client
/// .send(
Expand Down Expand Up @@ -279,13 +279,13 @@ impl Client {
/// Create a new transaction
#[inline]
pub fn create_transaction(&self) -> Transaction {
Transaction::new(self.clone())
Transaction::new(self)
}

/// Create a new pipeline
#[inline]
pub fn create_pipeline(&self) -> Pipeline {
Pipeline::new(self.clone())
Pipeline::new(self)
}

pub fn create_client_tracking_invalidation_stream(
Expand Down
26 changes: 13 additions & 13 deletions src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use rustis::{
#[cfg_attr(feature = "tokio-runtime", tokio::main)]
#[cfg_attr(feature = "async-std-runtime", async_std::main)]
async fn main() -> Result<()> {
let mut client = Client::connect("127.0.0.1:6379").await?;
let client = Client::connect("127.0.0.1:6379").await?;
client.flushdb(FlushingMode::Sync).await?;
client.set("key", "value").await?;
Expand Down Expand Up @@ -87,8 +87,8 @@ use rustis::{
#[cfg_attr(feature = "async-std-runtime", async_std::main)]
async fn main() -> Result<()> {
let config = "127.0.0.1:6379".into_config()?;
let mut regular_client1 = Client::connect(config.clone()).await?;
let mut pub_sub_client = Client::connect(config).await?;
let regular_client1 = Client::connect(config.clone()).await?;
let pub_sub_client = Client::connect(config).await?;
regular_client1.flushdb(FlushingMode::Sync).await?;
Expand All @@ -97,12 +97,12 @@ async fn main() -> Result<()> {
println!("value: {value:?}");
// clone a second instance on the same underlying connection
let mut regular_client2 = regular_client1.clone();
let regular_client2 = regular_client1.clone();
let value: String = regular_client2.get("key").await?;
println!("value: {value:?}");
// use 2nd connection to manager subscriptions
let mut pub_sub_stream = pub_sub_client.subscribe("my_channel").await?;
let pub_sub_stream = pub_sub_client.subscribe("my_channel").await?;
pub_sub_stream.close().await?;
Ok(())
Expand Down Expand Up @@ -137,12 +137,12 @@ async fn main() -> Result<()> {
.max_size(10)
.build(manager).await?;
let mut client1 = pool.get().await.unwrap();
let client1 = pool.get().await.unwrap();
client1.set("key1", "value1").await?;
let value: String = client1.get("key1").await?;
println!("value: {value:?}");
let mut client2 = pool.get().await.unwrap();
let client2 = pool.get().await.unwrap();
client2.set("key2", "value2").await?;
let value: String = client2.get("key2").await?;
println!("value: {value:?}");
Expand Down Expand Up @@ -229,7 +229,7 @@ use rustis::{client::Client, resp::cmd, Result};
#[cfg_attr(feature = "async-std-runtime", async_std::main)]
async fn main() -> Result<()> {
// standalone, host=localhost, port=6379 (default), database=1
let mut client = Client::connect("redis://localhost/1").await?;
let client = Client::connect("redis://localhost/1").await?;
Ok(())
}
Expand Down Expand Up @@ -273,7 +273,7 @@ use rustis::{
#[cfg_attr(feature = "tokio-runtime", tokio::main)]
#[cfg_attr(feature = "async-std-runtime", async_std::main)]
async fn main() -> Result<()> {
let mut client = Client::connect("127.0.0.1:6379").await?;
let client = Client::connect("127.0.0.1:6379").await?;
let mut pipeline = client.create_pipeline();
pipeline.set("key1", "value1").forget();
Expand Down Expand Up @@ -329,7 +329,7 @@ use rustis::{
#[cfg_attr(feature = "tokio-runtime", tokio::main)]
#[cfg_attr(feature = "async-std-runtime", async_std::main)]
async fn main() -> Result<()> {
let mut client = Client::connect("127.0.0.1:6379").await?;
let client = Client::connect("127.0.0.1:6379").await?;
let mut transaction = client.create_transaction();
Expand Down Expand Up @@ -386,8 +386,8 @@ use futures::StreamExt;
#[cfg_attr(feature = "tokio-runtime", tokio::main)]
#[cfg_attr(feature = "async-std-runtime", async_std::main)]
async fn main() -> Result<()> {
let mut subscribing_client = Client::connect("127.0.0.1:6379").await?;
let mut regular_client = Client::connect("127.0.0.1:6379").await?;
let subscribing_client = Client::connect("127.0.0.1:6379").await?;
let regular_client = Client::connect("127.0.0.1:6379").await?;
// cleanup
regular_client.flushdb(FlushingMode::Sync).await?;
Expand Down Expand Up @@ -429,7 +429,7 @@ use futures::StreamExt;
#[cfg_attr(feature = "tokio-runtime", tokio::main)]
#[cfg_attr(feature = "async-std-runtime", async_std::main)]
async fn main() -> Result<()> {
let mut subscribing_client = Client::connect("127.0.0.1:6379").await?;
let subscribing_client = Client::connect("127.0.0.1:6379").await?;
// 1st subscription
let mut pub_sub_stream = subscribing_client.subscribe("mychannel1").await?;
Expand Down
58 changes: 29 additions & 29 deletions src/client/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ use serde::de::DeserializeOwned;
use std::iter::zip;

/// Represents a Redis command pipeline.
pub struct Pipeline {
client: Client,
pub struct Pipeline<'a> {
client: &'a Client,
commands: Vec<Command>,
forget_flags: Vec<bool>,
retry_on_error: Option<bool>,
}

impl Pipeline {
pub(crate) fn new(client: Client) -> Pipeline {
impl<'a> Pipeline<'a> {
pub(crate) fn new(client: &'a Client) -> Pipeline {
Pipeline {
client,
commands: Vec::new(),
Expand Down Expand Up @@ -80,7 +80,7 @@ impl Pipeline {
/// #[cfg_attr(feature = "tokio-runtime", tokio::main)]
/// #[cfg_attr(feature = "async-std-runtime", async_std::main)]
/// async fn main() -> Result<()> {
/// let mut client = Client::connect("127.0.0.1:6379").await?;
/// let client = Client::connect("127.0.0.1:6379").await?;
///
/// let mut pipeline = client.create_pipeline();
/// pipeline.set("key1", "value1").forget();
Expand Down Expand Up @@ -131,7 +131,7 @@ pub trait BatchPreparedCommand<R = ()> {
fn forget(self);
}

impl<'a, R: Response> BatchPreparedCommand for PreparedCommand<'a, &'a mut Pipeline, R> {
impl<'a, 'b, R: Response> BatchPreparedCommand for PreparedCommand<'a, &'a mut Pipeline<'b>, R> {
/// Queue a command.
#[inline]
fn queue(self) {
Expand All @@ -145,44 +145,44 @@ impl<'a, R: Response> BatchPreparedCommand for PreparedCommand<'a, &'a mut Pipel
}
}

impl<'a> BitmapCommands<'a> for &'a mut Pipeline {}
impl<'a, 'b> BitmapCommands<'a> for &'a mut Pipeline<'b> {}
#[cfg_attr(docsrs, doc(cfg(feature = "redis-bloom")))]
#[cfg(feature = "redis-bloom")]
impl<'a> BloomCommands<'a> for &'a mut Pipeline {}
impl<'a> ClusterCommands<'a> for &'a mut Pipeline {}
impl<'a> ConnectionCommands<'a> for &'a mut Pipeline {}
impl<'a, 'b> BloomCommands<'a> for &'a mut Pipeline<'b> {}
impl<'a, 'b> ClusterCommands<'a> for &'a mut Pipeline<'b> {}
impl<'a, 'b> ConnectionCommands<'a> for &'a mut Pipeline<'b> {}
#[cfg_attr(docsrs, doc(cfg(feature = "redis-bloom")))]
#[cfg(feature = "redis-bloom")]
impl<'a> CountMinSketchCommands<'a> for &'a mut Pipeline {}
impl<'a, 'b> CountMinSketchCommands<'a> for &'a mut Pipeline<'b> {}
#[cfg_attr(docsrs, doc(cfg(feature = "redis-bloom")))]
#[cfg(feature = "redis-bloom")]
impl<'a> CuckooCommands<'a> for &'a mut Pipeline {}
impl<'a> GenericCommands<'a> for &'a mut Pipeline {}
impl<'a> GeoCommands<'a> for &'a mut Pipeline {}
impl<'a, 'b> CuckooCommands<'a> for &'a mut Pipeline<'b> {}
impl<'a, 'b> GenericCommands<'a> for &'a mut Pipeline<'b> {}
impl<'a, 'b> GeoCommands<'a> for &'a mut Pipeline<'b> {}
#[cfg_attr(docsrs, doc(cfg(feature = "redis-graph")))]
#[cfg(feature = "redis-graph")]
impl<'a> GraphCommands<'a> for &'a mut Pipeline {}
impl<'a> HashCommands<'a> for &'a mut Pipeline {}
impl<'a> HyperLogLogCommands<'a> for &'a mut Pipeline {}
impl<'a, 'b> GraphCommands<'a> for &'a mut Pipeline<'b> {}
impl<'a, 'b> HashCommands<'a> for &'a mut Pipeline<'b> {}
impl<'a, 'b> HyperLogLogCommands<'a> for &'a mut Pipeline<'b> {}
#[cfg_attr(docsrs, doc(cfg(feature = "redis-json")))]
#[cfg(feature = "redis-json")]
impl<'a> JsonCommands<'a> for &'a mut Pipeline {}
impl<'a> ListCommands<'a> for &'a mut Pipeline {}
impl<'a, 'b> JsonCommands<'a> for &'a mut Pipeline<'b> {}
impl<'a, 'b> ListCommands<'a> for &'a mut Pipeline<'b> {}
#[cfg_attr(docsrs, doc(cfg(feature = "redis-search")))]
#[cfg(feature = "redis-search")]
impl<'a> SearchCommands<'a> for &'a mut Pipeline {}
impl<'a> SetCommands<'a> for &'a mut Pipeline {}
impl<'a> ScriptingCommands<'a> for &'a mut Pipeline {}
impl<'a> ServerCommands<'a> for &'a mut Pipeline {}
impl<'a> SortedSetCommands<'a> for &'a mut Pipeline {}
impl<'a> StreamCommands<'a> for &'a mut Pipeline {}
impl<'a> StringCommands<'a> for &'a mut Pipeline {}
impl<'a, 'b> SearchCommands<'a> for &'a mut Pipeline<'b> {}
impl<'a, 'b> SetCommands<'a> for &'a mut Pipeline<'b> {}
impl<'a, 'b> ScriptingCommands<'a> for &'a mut Pipeline<'b> {}
impl<'a, 'b> ServerCommands<'a> for &'a mut Pipeline<'b> {}
impl<'a, 'b> SortedSetCommands<'a> for &'a mut Pipeline<'b> {}
impl<'a, 'b> StreamCommands<'a> for &'a mut Pipeline<'b> {}
impl<'a, 'b> StringCommands<'a> for &'a mut Pipeline<'b> {}
#[cfg_attr(docsrs, doc(cfg(feature = "redis-bloom")))]
#[cfg(feature = "redis-bloom")]
impl<'a> TDigestCommands<'a> for &'a mut Pipeline {}
impl<'a, 'b> TDigestCommands<'a> for &'a mut Pipeline<'b> {}
#[cfg_attr(docsrs, doc(cfg(feature = "redis-time-series")))]
#[cfg(feature = "redis-time-series")]
impl<'a> TimeSeriesCommands<'a> for &'a mut Pipeline {}
impl<'a, 'b> TimeSeriesCommands<'a> for &'a mut Pipeline<'b> {}
#[cfg_attr(docsrs, doc(cfg(feature = "redis-bloom")))]
#[cfg(feature = "redis-bloom")]
impl<'a> TopKCommands<'a> for &'a mut Pipeline {}
impl<'a, 'b> TopKCommands<'a> for &'a mut Pipeline<'b> {}
54 changes: 27 additions & 27 deletions src/client/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ use crate::{
use std::{fmt, marker::PhantomData};

/// Represents an on-going [`transaction`](https://redis.io/docs/manual/transactions/) on a specific client instance.
pub struct Transaction {
client: Client,
pub struct Transaction<'a> {
client: &'a Client,
commands: Vec<Command>,
forget_flags: Vec<bool>,
retry_on_error: Option<bool>,
}

impl Transaction {
pub(crate) fn new(client: Client) -> Self {
impl<'a> Transaction<'a> {
pub(crate) fn new(client: &'a Client) -> Self {
Self {
client,
commands: vec![cmd("MULTI")],
Expand Down Expand Up @@ -85,7 +85,7 @@ impl Transaction {
/// #[cfg_attr(feature = "tokio-runtime", tokio::main)]
/// #[cfg_attr(feature = "async-std-runtime", async_std::main)]
/// async fn main() -> Result<()> {
/// let mut client = Client::connect("127.0.0.1:6379").await?;
/// let client = Client::connect("127.0.0.1:6379").await?;
///
/// let mut transaction = client.create_transaction();
///
Expand Down Expand Up @@ -262,7 +262,7 @@ where
}
}

impl<'a, R: Response> BatchPreparedCommand for PreparedCommand<'a, &'a mut Transaction, R> {
impl<'a, 'b, R: Response> BatchPreparedCommand for PreparedCommand<'a, &'a mut Transaction<'b>, R> {
/// Queue a command into the transaction.
fn queue(self) {
self.executor.queue(self.command)
Expand All @@ -274,42 +274,42 @@ impl<'a, R: Response> BatchPreparedCommand for PreparedCommand<'a, &'a mut Trans
}
}

impl<'a> BitmapCommands<'a> for &'a mut Transaction {}
impl<'a, 'b> BitmapCommands<'a> for &'a mut Transaction<'b> {}
#[cfg_attr(docsrs, doc(cfg(feature = "redis-bloom")))]
#[cfg(feature = "redis-bloom")]
impl<'a> BloomCommands<'a> for &'a mut Transaction {}
impl<'a, 'b> BloomCommands<'a> for &'a mut Transaction<'b> {}
#[cfg_attr(docsrs, doc(cfg(feature = "redis-bloom")))]
#[cfg(feature = "redis-bloom")]
impl<'a> CountMinSketchCommands<'a> for &'a mut Transaction {}
impl<'a, 'b> CountMinSketchCommands<'a> for &'a mut Transaction<'b> {}
#[cfg_attr(docsrs, doc(cfg(feature = "redis-bloom")))]
#[cfg(feature = "redis-bloom")]
impl<'a> CuckooCommands<'a> for &'a mut Transaction {}
impl<'a> GenericCommands<'a> for &'a mut Transaction {}
impl<'a> GeoCommands<'a> for &'a mut Transaction {}
impl<'a, 'b> CuckooCommands<'a> for &'a mut Transaction<'b> {}
impl<'a, 'b> GenericCommands<'a> for &'a mut Transaction<'b> {}
impl<'a, 'b> GeoCommands<'a> for &'a mut Transaction<'b> {}
#[cfg_attr(docsrs, doc(cfg(feature = "redis-graph")))]
#[cfg(feature = "redis-graph")]
impl<'a> GraphCommands<'a> for &'a mut Transaction {}
impl<'a> HashCommands<'a> for &'a mut Transaction {}
impl<'a> HyperLogLogCommands<'a> for &'a mut Transaction {}
impl<'a, 'b> GraphCommands<'a> for &'a mut Transaction<'b> {}
impl<'a, 'b> HashCommands<'a> for &'a mut Transaction<'b> {}
impl<'a, 'b> HyperLogLogCommands<'a> for &'a mut Transaction<'b> {}
#[cfg_attr(docsrs, doc(cfg(feature = "redis-json")))]
#[cfg(feature = "redis-json")]
impl<'a> JsonCommands<'a> for &'a mut Transaction {}
impl<'a> ListCommands<'a> for &'a mut Transaction {}
impl<'a, 'b> JsonCommands<'a> for &'a mut Transaction<'b> {}
impl<'a, 'b> ListCommands<'a> for &'a mut Transaction<'b> {}
#[cfg_attr(docsrs, doc(cfg(feature = "redis-search")))]
#[cfg(feature = "redis-search")]
impl<'a> SearchCommands<'a> for &'a mut Transaction {}
impl<'a> SetCommands<'a> for &'a mut Transaction {}
impl<'a> ScriptingCommands<'a> for &'a mut Transaction {}
impl<'a> ServerCommands<'a> for &'a mut Transaction {}
impl<'a> SortedSetCommands<'a> for &'a mut Transaction {}
impl<'a> StreamCommands<'a> for &'a mut Transaction {}
impl<'a> StringCommands<'a> for &'a mut Transaction {}
impl<'a, 'b> SearchCommands<'a> for &'a mut Transaction<'b> {}
impl<'a, 'b> SetCommands<'a> for &'a mut Transaction<'b> {}
impl<'a, 'b> ScriptingCommands<'a> for &'a mut Transaction<'b> {}
impl<'a, 'b> ServerCommands<'a> for &'a mut Transaction<'b> {}
impl<'a, 'b> SortedSetCommands<'a> for &'a mut Transaction<'b> {}
impl<'a, 'b> StreamCommands<'a> for &'a mut Transaction<'b> {}
impl<'a, 'b> StringCommands<'a> for &'a mut Transaction<'b> {}
#[cfg_attr(docsrs, doc(cfg(feature = "redis-bloom")))]
#[cfg(feature = "redis-bloom")]
impl<'a> TDigestCommands<'a> for &'a mut Transaction {}
impl<'a, 'b> TDigestCommands<'a> for &'a mut Transaction<'b> {}
#[cfg_attr(docsrs, doc(cfg(feature = "redis-time-series")))]
#[cfg(feature = "redis-time-series")]
impl<'a> TimeSeriesCommands<'a> for &'a mut Transaction {}
impl<'a, 'b> TimeSeriesCommands<'a> for &'a mut Transaction<'b> {}
#[cfg_attr(docsrs, doc(cfg(feature = "redis-bloom")))]
#[cfg(feature = "redis-bloom")]
impl<'a> TopKCommands<'a> for &'a mut Transaction {}
impl<'a, 'b> TopKCommands<'a> for &'a mut Transaction<'b> {}
2 changes: 1 addition & 1 deletion src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ use rustis::{
#[cfg_attr(feature = "tokio-runtime", tokio::main)]
#[cfg_attr(feature = "async-std-runtime", async_std::main)]
async fn main() -> Result<()> {
let mut client = Client::connect("127.0.0.1:6379").await?;
let client = Client::connect("127.0.0.1:6379").await?;
// Send & await ListCommands::lpush command
let _size = client.lpush("mylist", ["element1", "element2"]).await?;
Expand Down
4 changes: 2 additions & 2 deletions src/commands/string_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ pub trait StringCommands<'a> {
/// #[cfg_attr(feature = "tokio-runtime", tokio::main)]
/// #[cfg_attr(feature = "async-std-runtime", async_std::main)]
/// async fn main() -> Result<()> {
/// let mut client = Client::connect("127.0.0.1:6379").await?;
/// let client = Client::connect("127.0.0.1:6379").await?;
/// client.flushdb(FlushingMode::Sync).await?;
///
/// // return value can be an Option<String>...
Expand Down Expand Up @@ -176,7 +176,7 @@ pub trait StringCommands<'a> {
/// #[cfg_attr(feature = "tokio-runtime", tokio::main)]
/// #[cfg_attr(feature = "async-std-runtime", async_std::main)]
/// async fn main() -> Result<()> {
/// let mut client = Client::connect("127.0.0.1:6379").await?;
/// let client = Client::connect("127.0.0.1:6379").await?;
/// client.flushdb(FlushingMode::Sync).await?;
///
/// client.set("key", "value").await?;
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ use rustis::{
#[cfg_attr(feature = "async-std-runtime", async_std::main)]
async fn main() -> Result<()> {
// Connect the client to a Redis server from its IP and port
let mut client = Client::connect("127.0.0.1:6379").await?;
let client = Client::connect("127.0.0.1:6379").await?;
// Flush all existing data in Redis
client.flushdb(FlushingMode::Sync).await?;
Expand Down Expand Up @@ -121,7 +121,7 @@ use rustis::{client::Client, resp::cmd, Result};
#[cfg_attr(feature = "tokio-runtime", tokio::main)]
#[cfg_attr(feature = "async-std-runtime", async_std::main)]
async fn main() -> Result<()> {
let mut client = Client::connect("127.0.0.1:6379").await?;
let client = Client::connect("127.0.0.1:6379").await?;
client
.send(
Expand Down
Loading

0 comments on commit 5946ba2

Please sign in to comment.