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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#![warn(missing_docs)]
macro_rules! code_name { () => { "udsx" }}
macro_rules! version { () => { "0.8.0" }}
pub const NAME: &str = "UDSx";
pub const CODE_NAME: &str = code_name!();
pub const ID: &str = concat!(
"3df68c92-3aef9c8a-620269e5-31b3c45d-0dd34f4c-2a7dce4e-33c12966-4e5631cb-",
"617e5279-d901a7b2-593e6f06-5e9c1de0-f4af1b84-f3057770-b6558c66-da0d4efe",
);
pub const VERSION: &str = version!();
pub const RELEASE_DATE: (u16, u8, u8) = (2019, 8, 19);
pub const TAG: &str = concat!(code_name!(), "::3df68c92::", version!());
use std::{
ffi::CStr,
io::{Error, ErrorKind},
mem,
};
pub mod version_info;
#[cfg(any(target_os = "linux", target_os = "l4re"))]
pub mod als;
#[cfg(any(target_os = "linux", target_os = "l4re"))]
mod credentials;
#[cfg(any(target_os = "linux", target_os = "l4re"))]
pub use credentials::*;
#[cfg(unix)]
mod udsx_unix_stream;
#[cfg(unix)]
pub use udsx_unix_stream::*;
pub type Result<T> = std::io::Result<T>;
#[test]
fn test_crate_version() {
assert_eq!(VERSION, env!("CARGO_PKG_VERSION"));
}
#[cfg(unix)]
unsafe fn format_errno<S>(libc_fn_name: S, errno: Option<i32>) -> String where S: AsRef<str> {
let libc_fn_name = libc_fn_name.as_ref();
let errno = errno.unwrap_or_else(|| libc_errno());
format!(
"Failed calling libc::{}{} -- *possible* errno: {:?} -> {:?}",
libc_fn_name, match libc_fn_name.ends_with(')') { true => concat!(), false => "()" },
errno, CStr::from_ptr(libc::strerror(errno)),
)
}
#[cfg(any(target_os = "linux", target_os = "l4re"))]
unsafe fn libc_errno() -> i32 {
*libc::__errno_location()
}
#[cfg(target_os = "netbsd")]
unsafe fn libc_errno() -> i32 {
*libc::__errno()
}
#[cfg(not(any(target_os = "linux", target_os = "l4re", target_os = "netbsd")))]
unsafe fn libc_errno() -> i32 {
*libc::__error()
}
fn usize_as_u32(n: usize) -> Result<u32> {
match mem::size_of::<usize>() <= mem::size_of::<u32>() || n <= u32::max_value() as usize {
true => Ok(n as u32),
false => Err(Error::new(ErrorKind::InvalidData, format!("Cannot cast {}_usize as u32", n))),
}
}
fn u32_as_usize(n: u32) -> Result<usize> {
match mem::size_of::<u32>() <= mem::size_of::<usize>() || n <= usize::max_value() as u32 {
true => Ok(n as usize),
false => Err(Error::new(ErrorKind::InvalidData, format!("Cannot cast {}_u32 as usize", n))),
}
}