Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

z_bytes: expose wrap and rename new to from_str #235

Merged
merged 4 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions include/zenoh_commons.h
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,12 @@ ZENOHC_API struct z_attachment_t z_attachment_null(void);
* Returns ``true`` if `b` is initialized.
*/
ZENOHC_API bool z_bytes_check(const struct z_bytes_t *b);
/**
* Returns a view of `str` using `strlen` (this should therefore not be used with untrusted inputs).
*
* `str == NULL` will cause this to return `z_bytes_null()`
*/
ZENOHC_API struct z_bytes_t z_bytes_from_str(const char *str);
/**
* Aliases `this` into a generic `z_attachment_t`, allowing it to be passed to corresponding APIs.
*/
Expand Down Expand Up @@ -1031,16 +1037,14 @@ ZENOHC_API struct z_owned_bytes_map_t z_bytes_map_new(void);
* Constructs the gravestone value for `z_owned_bytes_map_t`
*/
ZENOHC_API struct z_owned_bytes_map_t z_bytes_map_null(void);
/**
* Returns a view of `str` using `strlen`.
*
* `str == NULL` will cause this to return `z_bytes_null()`
*/
ZENOHC_API struct z_bytes_t z_bytes_new(const char *str);
/**
* Returns the gravestone value for `z_bytes_t`
*/
ZENOHC_API struct z_bytes_t z_bytes_null(void);
/**
* Constructs a `len` bytes long view starting at `start`.
*/
ZENOHC_API struct z_bytes_t z_bytes_wrap(const uint8_t *start, size_t len);
/**
* Closes a zenoh session. This drops and invalidates `session` for double-drop safety.
*
Expand Down
15 changes: 13 additions & 2 deletions src/collections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ pub extern "C" fn z_bytes_null() -> z_bytes_t {
}
}

/// Returns a view of `str` using `strlen`.
/// Returns a view of `str` using `strlen` (this should therefore not be used with untrusted inputs).
///
/// `str == NULL` will cause this to return `z_bytes_null()`
#[no_mangle]
#[allow(clippy::missing_safety_doc)]
pub unsafe extern "C" fn z_bytes_new(str: *const c_char) -> z_bytes_t {
pub unsafe extern "C" fn z_bytes_from_str(str: *const c_char) -> z_bytes_t {
if str.is_null() {
z_bytes_null()
} else {
Expand All @@ -78,6 +78,17 @@ pub unsafe extern "C" fn z_bytes_new(str: *const c_char) -> z_bytes_t {
}
}

/// Constructs a `len` bytes long view starting at `start`.
#[no_mangle]
#[allow(clippy::missing_safety_doc)]
pub unsafe extern "C" fn z_bytes_wrap(start: *const u8, len: usize) -> z_bytes_t {
if start.is_null() {
z_bytes_null()
} else {
z_bytes_t { len, start }
}
}

/// Frees `b` and invalidates it for double-drop safety.
#[allow(clippy::missing_safety_doc)]
pub(crate) unsafe fn z_bytes_drop(b: &mut z_bytes_t) {
Expand Down
Loading