Debian Codegraph @

third_party/rust/pulse/src/util.rs

raw
1 // Copyright © 2017 Mozilla Foundation
2 //
3 // This program is made available under an ISC-style license. See the
4 // accompanying file LICENSE for details.
5
6 use std::ffi::CStr;
7 use std::os::raw::c_char;
8 use std::ptr;
9
10 pub trait UnwrapCStr {
11 fn unwrap_cstr(self) -> *const c_char;
12 }
13
14 impl<'a, U> UnwrapCStr for U
15 where
16 U: Into<Option<&'a CStr>>,
17 {
18 fn unwrap_cstr(self) -> *const c_char {
19 self.into().map(|o| o.as_ptr()).unwrap_or(0 as *const _)
20 }
21 }
22
23 pub fn map_to_mut_ptr<T, U, F: FnOnce(&T) -> *mut U>(t: Option<&mut T>, f: F) -> *mut U {
24 match t {
25 Some(x) => f(x),
26 None => ptr::null_mut(),
27 }
28 }
29
30 pub fn str_to_ptr(s: Option<&CStr>) -> *const c_char {
31 match s {
32 Some(x) => x.as_ptr(),
33 None => ptr::null(),
34 }
35 }
36
37 pub fn to_ptr<T>(t: Option<&T>) -> *const T {
38 match t {
39 Some(x) => x as *const T,
40 None => ptr::null(),
41 }
42 }