pub struct Client { /* private fields */ }
Expand description

Established connection with a MTProto server.

Backed by a single TcpStream, Client provides basic network client functionality (no pooling, retrying, …). Connections are established using the connect function.

Requests are issued using the various methods of Client.

Implementations§

source§

impl Client

source

pub async fn get(&mut self, key: &str) -> Result<Option<Bytes>>

Get the value of key.

If the key does not exist the special value None is returned.

Examples

Demonstrates basic usage.

use mini_telegram::client;

#[tokio::main]
async fn main() {
    let mut client = client::connect("localhost:6379").await.unwrap();

    let val = client.get("foo").await.unwrap();
    println!("Got = {:?}", val);
}
source

pub async fn set(&mut self, key: &str, value: Bytes) -> Result<()>

Set key to hold the given value.

The value is associated with key until it is overwritten by the next call to set or it is removed.

If key already holds a value, it is overwritten. Any previous time to live associated with the key is discarded on successful SET operation.

Examples

Demonstrates basic usage.

use mini_telegram::client;

#[tokio::main]
async fn main() {
    let mut client = client::connect("localhost:6379").await.unwrap();

    client.set("foo", "bar".into()).await.unwrap();

    // Getting the value immediately works
    let val = client.get("foo").await.unwrap().unwrap();
    assert_eq!(val, "bar");
}
source

pub async fn set_expires(
&mut self,
key: &str,
value: Bytes,
expiration: Duration
) -> Result<()>

Set key to hold the given value. The value expires after expiration

The value is associated with key until one of the following:

  • it expires.
  • it is overwritten by the next call to set.
  • it is removed.

If key already holds a value, it is overwritten. Any previous time to live associated with the key is discarded on a successful SET operation.

Examples

Demonstrates basic usage. This example is not guaranteed to always work as it relies on time based logic and assumes the client and server stay relatively synchronized in time. The real world tends to not be so favorable.

use mini_telegram::client;
use tokio::time;
use std::time::Duration;

#[tokio::main]
async fn main() {
    let ttl = Duration::from_millis(500);
    let mut client = client::connect("localhost:6379").await.unwrap();

    client.set_expires("foo", "bar".into(), ttl).await.unwrap();

    // Getting the value immediately works
    let val = client.get("foo").await.unwrap().unwrap();
    assert_eq!(val, "bar");

    // Wait for the TTL to expire
    time::sleep(ttl).await;

    let val = client.get("foo").await.unwrap();
    assert!(val.is_some());
}
source

pub async fn publish(&mut self, channel: &str, message: Bytes) -> Result<u64>

Posts message to the given channel.

Returns the number of subscribers currently listening on the channel. There is no guarantee that these subscribers receive the message as they may disconnect at any time.

Examples

Demonstrates basic usage.

use mini_telegram::client;

#[tokio::main]
async fn main() {
    let mut client = client::connect("localhost:6379").await.unwrap();

    let val = client.publish("foo", "bar".into()).await.unwrap();
    println!("Got = {:?}", val);
}
source

pub async fn subscribe(self, channels: Vec<String>) -> Result<Subscriber>

Subscribes the client to the specified channels.

Once a client issues a subscribe command, it may no longer issue any non-pub/sub commands. The function consumes self and returns a Subscriber.

The Subscriber value is used to receive messages as well as manage the list of channels the client is subscribed to.

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere
T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere
T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere
T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for Twhere
U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere
U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere
U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where
S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where
S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more