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
73
74
75
76
77
78
79
80
81
82
83
use std::{
error::{Error as StdError},
fmt::{Display, Formatter, Result as FmtResult},
result::{Result as StdResult},
os::raw::c_char,
};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Error {
FailedInit,
MismatchSize,
InvalidArg,
}
impl StdError for Error {}
impl Display for Error {
fn fmt(&self, f: &mut Formatter) -> FmtResult {
use self::Error::*;
match self {
FailedInit => "creation error".fmt(f),
MismatchSize => "data size mismatch".fmt(f),
InvalidArg => "invalid argument".fmt(f),
}
}
}
pub type Result<T> = StdResult<T, Error>;
pub type Status = Result<()>;
pub trait AsNativeStr {
fn as_native_str(&self) -> &'static str;
fn as_native_cstr(&self) -> *const c_char {
self.as_native_str().as_ptr() as *const _
}
fn as_rust_str(&self) -> &'static str {
let nt_str = self.as_native_str();
&nt_str[..nt_str.len()-1]
}
}
pub(crate) fn check_init<T>(ptr: *mut T) -> Status {
if ptr.is_null() {
Err(Error::FailedInit)
} else {
Ok(())
}
}