| 1 |
use ffi; |
| 2 |
|
| 3 |
use libc; |
| 4 |
|
| 5 |
use std::slice; |
| 6 |
use std::str; |
| 7 |
use std::ptr; |
| 8 |
use std::ffi::CStr; |
| •9 |
use std::os::raw::c_char; |
| 10 |
|
| 11 |
pub fn decode_c_str(c_str: *const ffi::yaml_char_t) -> Option<String> { |
| 12 |
if c_str == ptr::null() { |
| 13 |
None |
| 14 |
} else { |
| 15 |
unsafe { |
| •16 |
let c_char_str = c_str as *const c_char; |
| 17 |
str::from_utf8(CStr::from_ptr(c_char_str).to_bytes()).map(|s| s.to_string()).ok() |
| 18 |
} |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
pub fn decode_buf(buf: *const ffi::yaml_char_t, length: libc::size_t) -> Option<String> { |
| 23 |
if buf == ptr::null() { |
| 24 |
None |
| 25 |
} else { |
| 26 |
unsafe { |
| 27 |
str::from_utf8(slice::from_raw_parts(buf, length as usize)).map(|s| { s.to_string() }).ok() |
| 28 |
} |
| 29 |
} |
| 30 |
} |