zenoh_ext/
session_ext.rs

1//
2// Copyright (c) 2023 ZettaScale Technology
3//
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License 2.0 which is available at
6// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
7// which is available at https://www.apache.org/licenses/LICENSE-2.0.
8//
9// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
10//
11// Contributors:
12//   ZettaScale Zenoh Team, <zenoh@zettascale.tech>
13//
14
15use zenoh::{key_expr::KeyExpr, session::Session, Error};
16
17#[allow(deprecated)]
18use super::PublicationCacheBuilder;
19
20/// Some extensions to the [`zenoh::Session`](zenoh::Session)
21#[zenoh_macros::unstable]
22#[allow(deprecated)]
23pub trait SessionExt {
24    // REVIEW(fuzzypixelz): this doc test is the only one to use the programmatic configuration API..
25    /// Declare a [`PublicationCache`](crate::PublicationCache).
26    ///
27    /// Examples:
28    /// ```
29    /// # #[tokio::main]
30    /// # async fn main() {
31    /// use zenoh_ext::SessionExt;
32    /// use zenoh_config::ModeDependentValue::Unique;
33    ///
34    ///
35    /// let mut config = zenoh::Config::default();
36    /// config.timestamping.set_enabled(Some(Unique(true)));
37    /// let session = zenoh::open(config).await.unwrap();
38    /// let publication_cache = session.declare_publication_cache("key/expression").await.unwrap();
39    /// tokio::task::spawn(async move {
40    ///     publication_cache.key_expr();
41    /// }).await;
42    /// # }
43    /// ```
44    #[deprecated = "Use `AdvancedPublisher` and `AdvancedSubscriber` instead."]
45    #[zenoh_macros::unstable]
46    fn declare_publication_cache<'a, 'b, 'c, TryIntoKeyExpr>(
47        &'a self,
48        pub_key_expr: TryIntoKeyExpr,
49    ) -> PublicationCacheBuilder<'a, 'b, 'c>
50    where
51        TryIntoKeyExpr: TryInto<KeyExpr<'b>>,
52        <TryIntoKeyExpr as TryInto<KeyExpr<'b>>>::Error: Into<Error>;
53}
54
55#[allow(deprecated)]
56impl SessionExt for Session {
57    #[zenoh_macros::unstable]
58    fn declare_publication_cache<'a, 'b, 'c, TryIntoKeyExpr>(
59        &'a self,
60        pub_key_expr: TryIntoKeyExpr,
61    ) -> PublicationCacheBuilder<'a, 'b, 'c>
62    where
63        TryIntoKeyExpr: TryInto<KeyExpr<'b>>,
64        <TryIntoKeyExpr as TryInto<KeyExpr<'b>>>::Error: Into<Error>,
65    {
66        PublicationCacheBuilder::new(self, pub_key_expr.try_into().map_err(Into::into))
67    }
68}