Macro libyml::yaml_strdup
source · macro_rules! yaml_strdup { ($str:expr) => { ... }; }
Expand description
Duplicates a string using the system’s malloc
function and manual copy.
This macro wraps the yaml_strdup
function, providing a more Rust-like interface.
§Arguments
str
- A pointer to the null-terminated string to duplicate.
§Returns
Returns a *mut yaml_char_t
pointer to the newly allocated string, or None
if the allocation failed or the input was null.
§Safety
While this macro provides a safer interface than direct use of yaml_strdup
,
it is still unsafe because:
- It involves memory allocation and raw pointer manipulation.
- The caller must ensure that
str
is a valid, null-terminated string. - The caller is responsible for freeing the returned pointer using
yaml_free!
.
§Examples
use libyml::yaml::yaml_char_t;
use libyml::{yaml_strdup, yaml_free};
unsafe {
let original = b"Hello, world!\0".as_ptr() as *const yaml_char_t;
if let Some(copy) = yaml_strdup!(original) {
// Use the duplicated string
// ...
yaml_free!(copy);
}
}