-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathutils.rs
72 lines (64 loc) · 1.68 KB
/
utils.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//
// Copyright (c) The yang-rs Core Contributors
//
// SPDX-License-Identifier: MIT
//
use libyang3_sys as ffi;
use std::ffi::CStr;
use std::os::raw::c_char;
/// Convert C String to owned string.
pub(crate) fn char_ptr_to_string(c_str: *const c_char, free: bool) -> String {
let string =
unsafe { CStr::from_ptr(c_str).to_string_lossy().into_owned() };
if free {
unsafe { ffi::free(c_str as *mut std::ffi::c_void) };
}
string
}
/// Convert C String to optional owned string.
pub(crate) fn char_ptr_to_opt_string(
c_str: *const c_char,
free: bool,
) -> Option<String> {
if c_str.is_null() {
None
} else {
Some(char_ptr_to_string(c_str, free))
}
}
/// Convert C String to string slice.
pub(crate) fn char_ptr_to_str<'a>(c_str: *const c_char) -> &'a str {
unsafe { CStr::from_ptr(c_str).to_str().unwrap() }
}
/// Convert C String to optional string slice.
pub(crate) fn char_ptr_to_opt_str<'a>(c_str: *const c_char) -> Option<&'a str> {
if c_str.is_null() {
None
} else {
Some(char_ptr_to_str(c_str))
}
}
/// A trait implemented by all types that can be created from a raw C pointer
/// and a generic container type.
pub unsafe trait Binding<'a>
where
Self: Sized,
<Self as Binding<'a>>::Container: 'a,
{
type CType;
type Container;
unsafe fn from_raw(
container: &'a Self::Container,
raw: *mut Self::CType,
) -> Self;
unsafe fn from_raw_opt(
container: &'a Self::Container,
raw: *mut Self::CType,
) -> Option<Self> {
if raw.is_null() {
None
} else {
Some(Self::from_raw(container, raw))
}
}
}