use crate::{
Result,
client::{
Client, ClientPreparedCommand, ClientTrackingInvalidationStream, CommandFuture, IntoConfig,
Pipeline, PreparedCommand, PubSubStream, Transaction, command_traits::*,
},
commands::{BlockingCommands, PubSubCommands, TransactionCommands},
network::ReconnectReceiver,
resp::{Command, Response},
};
use serde::{Serialize, de::DeserializeOwned};
use std::future::IntoFuture;
#[cfg(doc)]
use crate::client::MonitorStream;
pub struct ExclusiveClient {
inner: Client,
}
impl ExclusiveClient {
#[inline]
pub async fn connect(config: impl IntoConfig) -> Result<Self> {
Ok(Self::from_client(Client::connect(config).await?))
}
pub(crate) fn from_client(inner: Client) -> Self {
Self { inner }
}
pub(crate) fn inner(&self) -> &Client {
&self.inner
}
#[inline]
pub fn into_multiplexed(self) -> Client {
self.inner
}
#[inline]
pub async fn close(self) -> Result<()> {
self.inner.close().await
}
#[inline]
pub fn on_reconnect(&self) -> ReconnectReceiver {
self.inner.on_reconnect()
}
#[inline]
pub async fn send<T: DeserializeOwned>(
&self,
command: impl Into<Command>,
retry_on_error: Option<bool>,
) -> Result<T> {
self.inner.send(command, retry_on_error).await
}
#[inline]
pub fn send_and_forget(
&self,
command: impl Into<Command>,
retry_on_error: Option<bool>,
) -> Result<()> {
self.inner.send_and_forget(command, retry_on_error)
}
#[inline]
pub fn create_transaction(&self) -> Transaction {
self.inner.create_transaction()
}
#[inline]
pub fn create_pipeline<'a>(&'a self) -> Pipeline<'a> {
self.inner.create_pipeline()
}
#[inline]
pub fn create_pub_sub(&self) -> PubSubStream {
self.inner.create_pub_sub()
}
#[inline]
pub fn create_client_tracking_invalidation_stream(
&self,
) -> Result<ClientTrackingInvalidationStream> {
self.inner.create_client_tracking_invalidation_stream()
}
}
impl<'a, R: Response> ClientPreparedCommand<'a, R> for PreparedCommand<'a, &'a ExclusiveClient, R> {
#[inline]
fn forget(self) -> Result<()> {
self.executor
.inner()
.send_and_forget(self.command, self.retry_on_error)
}
}
impl<'a, R: Response + DeserializeOwned + 'a> IntoFuture
for PreparedCommand<'a, &'a ExclusiveClient, R>
{
type Output = Result<R>;
type IntoFuture = CommandFuture<'a, R>;
#[inline]
fn into_future(self) -> Self::IntoFuture {
CommandFuture::new(self.executor.inner(), self.command, self.retry_on_error)
}
}
impl_shared_command_traits!(ExclusiveClient);
impl<'a> BlockingCommands<'a> for &'a ExclusiveClient {
#[inline]
async fn monitor(self) -> Result<crate::client::MonitorStream> {
self.inner.monitor_stream().await
}
}
impl<'a> TransactionCommands<'a> for &'a ExclusiveClient {}
impl<'a> PubSubCommands<'a> for &'a ExclusiveClient {
#[inline]
async fn subscribe(self, channels: impl Serialize) -> Result<PubSubStream> {
self.inner.subscribe(channels).await
}
#[inline]
async fn psubscribe(self, patterns: impl Serialize) -> Result<PubSubStream> {
self.inner.psubscribe(patterns).await
}
#[inline]
async fn ssubscribe(self, shardchannels: impl Serialize) -> Result<PubSubStream> {
self.inner.ssubscribe(shardchannels).await
}
}