Skip to content

Commit

Permalink
tags
Browse files Browse the repository at this point in the history
  • Loading branch information
milyin committed Feb 19, 2024
1 parent 01a6995 commit 1c1de85
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
13 changes: 13 additions & 0 deletions include/zenoh_commons.h
Original file line number Diff line number Diff line change
Expand Up @@ -542,12 +542,14 @@ typedef struct z_owned_closure_zid_t {
* After a move, `val` will still exist, but will no longer be valid. The destructors are double-drop-safe, but other functions will still trust that your `val` is valid.
*
* To check if `val` is still valid, you may use `z_X_check(&val)` or `z_check(val)` if your compiler supports `_Generic`, which will return `true` if `val` is valid.
* tags{c.z_owned_config_t, api.config}
*/
typedef struct z_owned_config_t {
void *_0;
} z_owned_config_t;
/**
* A loaned zenoh configuration.
* tags{c.z_config_t, api.config}
*/
typedef struct z_config_t {
const struct z_owned_config_t *_0;
Expand Down Expand Up @@ -1281,18 +1283,22 @@ ZENOHC_API bool z_config_check(const struct z_owned_config_t *config);
/**
* Constructs a default, zenoh-allocated, client mode configuration.
* If `peer` is not null, it is added to the configuration as remote peer.
* tags{c.z_config_client, api.config.create.client}
*/
ZENOHC_API struct z_owned_config_t z_config_client(const char *const *peers, size_t n_peers);
/**
* Creates a default, zenoh-allocated, configuration.
* tags{c.z_config_default, api.config.create.default}
*/
ZENOHC_API struct z_owned_config_t z_config_default(void);
/**
* Frees `config`, invalidating it for double-drop safety.
* tags{c.z_config_drop}
*/
ZENOHC_API void z_config_drop(struct z_owned_config_t *config);
/**
* Returns a :c:type:`z_config_t` loaned from `s`.
* tags{c.z_config_loan}
*/
ZENOHC_API struct z_config_t z_config_loan(const struct z_owned_config_t *s);
/**
Expand All @@ -1306,6 +1312,7 @@ ZENOHC_API struct z_config_t z_config_loan(const struct z_owned_config_t *s);
* After a move, `val` will still exist, but will no longer be valid. The destructors are double-drop-safe, but other functions will still trust that your `val` is valid.
*
* To check if `val` is still valid, you may use `z_X_check(&val)` or `z_check(val)` if your compiler supports `_Generic`, which will return `true` if `val` is valid.
* tags{c.z_config_new, api.config.create.empty}
*/
ZENOHC_API
struct z_owned_config_t z_config_new(void);
Expand All @@ -1315,6 +1322,7 @@ struct z_owned_config_t z_config_new(void);
ZENOHC_API struct z_owned_config_t z_config_null(void);
/**
* Constructs a default, zenoh-allocated, peer mode configuration.
* tags{c.z_config_peer, api.config.create.peer}
*/
ZENOHC_API struct z_owned_config_t z_config_peer(void);
/**
Expand Down Expand Up @@ -2147,19 +2155,22 @@ ZENOHC_API
int8_t z_undeclare_subscriber(struct z_owned_subscriber_t *sub);
/**
* Constructs a configuration by parsing a file at `path`. Currently supported format is JSON5, a superset of JSON.
* tags{c.z_config_from_file, api.config.create.from_file}
*/
ZENOHC_API
struct z_owned_config_t zc_config_from_file(const char *path);
/**
* Reads a configuration from a JSON-serialized string, such as '{mode:"client",connect:{endpoints:["tcp/127.0.0.1:7447"]}}'.
*
* Passing a null-ptr will result in a gravestone value (`z_check(x) == false`).
* tags{c.z_config_from_str, api.config.create.from_str}
*/
ZENOHC_API
struct z_owned_config_t zc_config_from_str(const char *s);
/**
* Gets the property with the given path key from the configuration, returning an owned, null-terminated, JSON serialized string.
* Use `z_drop` to safely deallocate this string
* tags{c.z_config_get}
*/
ZENOHC_API
struct z_owned_str_t zc_config_get(struct z_config_t config,
Expand All @@ -2168,13 +2179,15 @@ struct z_owned_str_t zc_config_get(struct z_config_t config,
* Inserts a JSON-serialized `value` at the `key` position of the configuration.
*
* Returns 0 if successful, a negative value otherwise.
* tags{c.z_config_insert_json}
*/
ZENOHC_API
int8_t zc_config_insert_json(struct z_config_t config,
const char *key,
const char *value);
/**
* Converts `config` into a JSON-serialized string, such as '{"mode":"client","connect":{"endpoints":["tcp/127.0.0.1:7447"]}}'.
* tags{c.z_config_to_string, api.config.to_string}
*/
ZENOHC_API
struct z_owned_str_t zc_config_to_string(struct z_config_t config);
Expand Down
2 changes: 2 additions & 0 deletions src/commons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ pub struct z_sample_t<'a> {
}

impl<'a> z_sample_t<'a> {
// tags{}
pub fn new(sample: &'a Sample, owner: &'a ZBuf) -> Self {
let std::borrow::Cow::Borrowed(payload) = owner.contiguous() else {
panic!("Attempted to construct z_sample_t from discontiguous buffer, this is definitely a bug in zenoh-c, please report it.")
Expand Down Expand Up @@ -467,6 +468,7 @@ pub struct z_owned_encoding_t {
}

impl z_owned_encoding_t {
// tags{}
pub fn null() -> Self {
z_owned_encoding_t {
prefix: z_encoding_prefix_t::Empty,
Expand Down
29 changes: 29 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,46 +18,62 @@ use zenoh::config::{Config, ValidatedMap, WhatAmI};
use crate::{impl_guarded_transmute, z_owned_str_t, z_str_null, GuardedTransmute};

#[no_mangle]
/// tags{ api.options.whatami}
/// tags{c.z_router, api.options.whatami.router}
pub static Z_ROUTER: c_uint = WhatAmI::Router as c_uint;
#[no_mangle]
/// tags{c.z_router, api.options.whatami.peer}
pub static Z_PEER: c_uint = WhatAmI::Peer as c_uint;
#[no_mangle]
/// tags{c.z_client, api.options.whatami.client}
pub static Z_CLIENT: c_uint = WhatAmI::Client as c_uint;

#[no_mangle]
/// tags{c.z_config_mode_key, api.config.mode}
pub static Z_CONFIG_MODE_KEY: &c_char = unsafe { &*(b"mode\0".as_ptr() as *const c_char) };
#[no_mangle]
/// tags{c.z_config_connect_key, api.config.connect}
pub static Z_CONFIG_CONNECT_KEY: &c_char =
unsafe { &*(b"connect/endpoints\0".as_ptr() as *const c_char) };
#[no_mangle]
/// tags{c.z_config_listen_key, api.config.listen}
pub static Z_CONFIG_LISTEN_KEY: &c_char =
unsafe { &*(b"listen/endpoints\0".as_ptr() as *const c_char) };
#[no_mangle]
/// tags{c.z_config_user_key, api.config.transport.auth.usrpwd.user}
pub static Z_CONFIG_USER_KEY: &c_char =
unsafe { &*(b"transport/auth/usrpwd/user\0".as_ptr() as *const c_char) };
#[no_mangle]
/// tags{c.z_config_password_key, api.config.transport.auth.usrpwd.password}
pub static Z_CONFIG_PASSWORD_KEY: &c_char =
unsafe { &*(b"transport/auth/usrpwd/password\0".as_ptr() as *const c_char) };
#[no_mangle]
/// tags{c.z_config_multicast_scouting_key, api.config.scouting.multicast.enabled}
pub static Z_CONFIG_MULTICAST_SCOUTING_KEY: &c_char =
unsafe { &*(b"scouting/multicast/enabled\0".as_ptr() as *const c_char) };
#[no_mangle]
/// tags{c.z_config_multicast_interface_key, api.config.scouting.multicast.interface}
pub static Z_CONFIG_MULTICAST_INTERFACE_KEY: &c_char =
unsafe { &*(b"scouting/multicast/interface\0".as_ptr() as *const c_char) };
#[no_mangle]
/// tags{c.z_config_multicast_ipv4_address_key, api.config.scouting.multicast.address}
pub static Z_CONFIG_MULTICAST_IPV4_ADDRESS_KEY: &c_char =
unsafe { &*(b"scouting/multicast/address\0".as_ptr() as *const c_char) };
#[no_mangle]
/// tags{c.z_config_multicast_port_key, api.config.scouting.multicast.port}
pub static Z_CONFIG_SCOUTING_TIMEOUT_KEY: &c_char =
unsafe { &*(b"scouting/timeout\0".as_ptr() as *const c_char) };
#[no_mangle]
/// tags{c.z_config_scouting_delay_key, api.config.scouting.delay}
pub static Z_CONFIG_SCOUTING_DELAY_KEY: &c_char =
unsafe { &*(b"scouting/delay\0".as_ptr() as *const c_char) };
#[no_mangle]
/// tags{c.z_config_add_timestamp_key, api.config.timestamping.enabled}
pub static Z_CONFIG_ADD_TIMESTAMP_KEY: &c_char =
unsafe { &*(b"timestamping/enabled\0".as_ptr() as *const c_char) };

/// A loaned zenoh configuration.
/// tags{c.z_config_t, api.config}
#[repr(C)]
#[allow(non_camel_case_types)]
pub struct z_config_t(*const z_owned_config_t);
Expand All @@ -72,6 +88,7 @@ pub struct z_config_t(*const z_owned_config_t);
/// After a move, `val` will still exist, but will no longer be valid. The destructors are double-drop-safe, but other functions will still trust that your `val` is valid.
///
/// To check if `val` is still valid, you may use `z_X_check(&val)` or `z_check(val)` if your compiler supports `_Generic`, which will return `true` if `val` is valid.
/// tags{c.z_owned_config_t, api.config}
#[repr(C)]
pub struct z_owned_config_t(*mut ());
impl_guarded_transmute!(Option<Box<Config>>, z_owned_config_t);
Expand All @@ -82,6 +99,7 @@ impl From<Option<Box<Config>>> for z_owned_config_t {
}
}
/// Returns a :c:type:`z_config_t` loaned from `s`.
/// tags{c.z_config_loan}
#[no_mangle]
pub extern "C" fn z_config_loan(s: &z_owned_config_t) -> z_config_t {
z_config_t(s)
Expand Down Expand Up @@ -122,20 +140,23 @@ impl z_owned_config_t {
/// After a move, `val` will still exist, but will no longer be valid. The destructors are double-drop-safe, but other functions will still trust that your `val` is valid.
///
/// To check if `val` is still valid, you may use `z_X_check(&val)` or `z_check(val)` if your compiler supports `_Generic`, which will return `true` if `val` is valid.
/// tags{c.z_config_new, api.config.create.empty}
#[no_mangle]
pub extern "C" fn z_config_new() -> z_owned_config_t {
let config: Box<Config> = Box::default();
unsafe { z_owned_config_t(std::mem::transmute(Some(config))) }
}

/// Constructs a null safe-to-drop value of 'z_owned_config_t' type
// tags{c.z_config_null}
#[no_mangle]
pub extern "C" fn z_config_null() -> z_owned_config_t {
z_owned_config_t::null()
}

/// Gets the property with the given path key from the configuration, returning an owned, null-terminated, JSON serialized string.
/// Use `z_drop` to safely deallocate this string
/// tags{c.z_config_get}
#[no_mangle]
#[allow(clippy::missing_safety_doc)]
pub unsafe extern "C" fn zc_config_get(config: z_config_t, key: *const c_char) -> z_owned_str_t {
Expand All @@ -154,6 +175,7 @@ pub unsafe extern "C" fn zc_config_get(config: z_config_t, key: *const c_char) -
/// Inserts a JSON-serialized `value` at the `key` position of the configuration.
///
/// Returns 0 if successful, a negative value otherwise.
/// tags{c.z_config_insert_json}
#[no_mangle]
#[allow(clippy::missing_safety_doc, unused_must_use)]
pub unsafe extern "C" fn zc_config_insert_json(
Expand All @@ -177,6 +199,7 @@ pub unsafe extern "C" fn zc_config_insert_json(
/// Frees `config`, invalidating it for double-drop safety.
#[no_mangle]
#[allow(clippy::missing_safety_doc)]
/// tags{c.z_config_drop}
pub extern "C" fn z_config_drop(config: &mut z_owned_config_t) {
std::mem::drop(config.as_mut().take())
}
Expand All @@ -188,6 +211,7 @@ pub extern "C" fn z_config_check(config: &z_owned_config_t) -> bool {
}

/// Creates a default, zenoh-allocated, configuration.
/// tags{c.z_config_default, api.config.create.default}
#[no_mangle]
#[allow(clippy::missing_safety_doc)]
pub extern "C" fn z_config_default() -> z_owned_config_t {
Expand All @@ -197,6 +221,7 @@ pub extern "C" fn z_config_default() -> z_owned_config_t {
/// Reads a configuration from a JSON-serialized string, such as '{mode:"client",connect:{endpoints:["tcp/127.0.0.1:7447"]}}'.
///
/// Passing a null-ptr will result in a gravestone value (`z_check(x) == false`).
/// tags{c.z_config_from_str, api.config.create.from_str}
#[no_mangle]
#[allow(clippy::missing_safety_doc)]
pub unsafe extern "C" fn zc_config_from_str(s: *const c_char) -> z_owned_config_t {
Expand All @@ -210,6 +235,7 @@ pub unsafe extern "C" fn zc_config_from_str(s: *const c_char) -> z_owned_config_
}

/// Converts `config` into a JSON-serialized string, such as '{"mode":"client","connect":{"endpoints":["tcp/127.0.0.1:7447"]}}'.
/// tags{c.z_config_to_string, api.config.to_string}
#[allow(clippy::missing_safety_doc)]
#[no_mangle]
pub extern "C" fn zc_config_to_string(config: z_config_t) -> z_owned_str_t {
Expand All @@ -224,6 +250,7 @@ pub extern "C" fn zc_config_to_string(config: z_config_t) -> z_owned_str_t {
}

/// Constructs a configuration by parsing a file at `path`. Currently supported format is JSON5, a superset of JSON.
/// tags{c.z_config_from_file, api.config.create.from_file}
#[allow(clippy::missing_safety_doc)]
#[no_mangle]
pub unsafe extern "C" fn zc_config_from_file(path: *const c_char) -> z_owned_config_t {
Expand All @@ -244,6 +271,7 @@ pub unsafe extern "C" fn zc_config_from_file(path: *const c_char) -> z_owned_con
}

/// Constructs a default, zenoh-allocated, peer mode configuration.
/// tags{c.z_config_peer, api.config.create.peer}
#[allow(clippy::missing_safety_doc)]
#[no_mangle]
pub extern "C" fn z_config_peer() -> z_owned_config_t {
Expand All @@ -252,6 +280,7 @@ pub extern "C" fn z_config_peer() -> z_owned_config_t {

/// Constructs a default, zenoh-allocated, client mode configuration.
/// If `peer` is not null, it is added to the configuration as remote peer.
/// tags{c.z_config_client, api.config.create.client}
#[allow(clippy::missing_safety_doc)]
#[no_mangle]
pub unsafe extern "C" fn z_config_client(
Expand Down

0 comments on commit 1c1de85

Please sign in to comment.