summaryrefslogtreecommitdiffstats
path: root/third_party/rust/pulse/src/util.rs
blob: 8f18a49857da9a941b80712748e1b7fa950ac1c9 (plain)
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
// Copyright © 2017 Mozilla Foundation
//
// This program is made available under an ISC-style license.  See the
// accompanying file LICENSE for details.

use std::ffi::CStr;
use std::os::raw::c_char;
use std::ptr;

pub trait UnwrapCStr {
    fn unwrap_cstr(self) -> *const c_char;
}

impl<'a, U> UnwrapCStr for U
where
    U: Into<Option<&'a CStr>>,
{
    fn unwrap_cstr(self) -> *const c_char {
        self.into().map(|o| o.as_ptr()).unwrap_or(0 as *const _)
    }
}

pub fn map_to_mut_ptr<T, U, F: FnOnce(&T) -> *mut U>(t: Option<&mut T>, f: F) -> *mut U {
    match t {
        Some(x) => f(x),
        None => ptr::null_mut(),
    }
}

pub fn str_to_ptr(s: Option<&CStr>) -> *const c_char {
    match s {
        Some(x) => x.as_ptr(),
        None => ptr::null(),
    }
}

pub fn to_ptr<T>(t: Option<&T>) -> *const T {
    match t {
        Some(x) => x as *const T,
        None => ptr::null(),
    }
}