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
pub trait Info: private::InfoImpl
where
Self::Type: private::FromInfoValue,
{
type Type;
}
pub(crate) mod private {
use std::os::raw::c_char;
pub unsafe trait InfoImpl {
const NAME: *const c_char;
}
#[allow(unreachable_pub)]
pub trait FromInfoValue: Sized {
fn from_info_value(info: Option<&str>) -> Self;
}
}
impl private::FromInfoValue for String {
fn from_info_value(info: Option<&str>) -> Self {
info.map(ToOwned::to_owned)
.unwrap_or_else(|| panic!("Unexpected null info value"))
}
}
impl private::FromInfoValue for Option<String> {
fn from_info_value(info: Option<&str>) -> Self {
info.map(ToOwned::to_owned)
}
}
macro_rules! info {
($struct_name:ident, $info_name:literal, $ty:ty, $description:literal) => {
#[doc = "`"]
#[doc = $info_name]
#[doc = "`"]
#[doc = ""]
#[doc = $description]
#[derive(Debug, Copy, Clone)]
pub struct $struct_name;
unsafe impl crate::info::private::InfoImpl for $struct_name {
const NAME: *const ::std::os::raw::c_char = concat!($info_name, "\0").as_ptr().cast();
}
impl crate::info::Info for $struct_name {
type Type = $ty;
}
};
}
mod impls;
pub use impls::*;