Skip to content

Commit

Permalink
Add (De)Serialize impls
Browse files Browse the repository at this point in the history
  • Loading branch information
fuzzypixelz committed Sep 17, 2024
1 parent bf67421 commit 1933f8f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
18 changes: 15 additions & 3 deletions zenoh/src/api/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::{
sync::{Arc, Mutex, MutexGuard},
};

use serde::{Deserialize, Serialize};
use zenoh_result::ZResult;

/// Zenoh configuration.
Expand All @@ -17,7 +18,7 @@ use zenoh_result::ZResult;
/// To construct a configuration, we advise that you use a configuration file (JSON, JSON5 and YAML
/// are currently supported, please use the proper extension for your format as the deserializer
/// will be picked according to it).
#[derive(Default, Debug, Clone)]
#[derive(Default, Debug, Clone, Serialize, Deserialize)]
pub struct Config(pub(crate) zenoh_config::Config);

impl Config {
Expand Down Expand Up @@ -48,6 +49,16 @@ impl Config {
pub fn remove<K: AsRef<str>>(&mut self, key: K) -> ZResult<()> {
self.0.remove(key)
}

#[zenoh_macros::unstable]
pub fn from_deserializer<'d, D: serde::Deserializer<'d>>(
d: D,
) -> Result<Self, Result<Self, D::Error>>
where
Self: serde::Deserialize<'d>,
{
Ok(Config(zenoh_config::Config::from_deserializer(d)?))
}
}

#[derive(Debug)]
Expand Down Expand Up @@ -165,7 +176,7 @@ impl Notifier<Config> {
}

pub fn remove<K: AsRef<str>>(&self, key: K) -> ZResult<()> {
self.lock_config().remove(key.as_ref())?;
self.lock_config().0.remove(key.as_ref())?;
self.notify(key);
Ok(())
}
Expand All @@ -174,10 +185,11 @@ impl Notifier<Config> {
self.lock_config().insert_json5(key, value)
}

#[allow(dead_code)]
pub fn get<'a>(&'a self, key: &str) -> Result<LookupGuard<'a, Config>, LookupError> {
let config = self.lock_config();
// SAFETY: MutexGuard pins the mutex behind which the value is held.
let subref = config.get(key.as_ref())? as *const _;
let subref = config.0.get(key.as_ref()).map_err(LookupError)? as *const _;
Ok(LookupGuard {
_guard: config,
subref,
Expand Down
1 change: 0 additions & 1 deletion zenoh/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,6 @@ pub mod config {
pub use zenoh_config::{WhatAmI, WhatAmIMatcher};

pub use crate::api::config::{Config, InsertionError};

#[zenoh_macros::unstable]
pub use crate::api::config::{LookupError, LookupGuard, Notifier};
}
Expand Down
5 changes: 4 additions & 1 deletion zenoh/src/net/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ use super::{primitives::DeMux, routing, routing::router::Router};
use crate::api::loader::{load_plugins, start_plugins};
#[cfg(feature = "plugins")]
use crate::api::plugins::PluginsManager;
use crate::{config::Notifier, Config, GIT_VERSION, LONG_VERSION};
use crate::{
api::config::{Config, Notifier},
GIT_VERSION, LONG_VERSION,
};

pub(crate) struct RuntimeState {
zid: ZenohId,
Expand Down

0 comments on commit 1933f8f

Please sign in to comment.