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
#![allow(deprecated)]
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use static_assertions::assert_impl_all;
use std::os::unix::io;
use crate::{Basic, EncodingFormat, Signature, Type};
#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)]
pub struct Fd(io::RawFd);
assert_impl_all!(Fd: Send, Sync, Unpin);
impl Basic for Fd {
const SIGNATURE_CHAR: char = 'h';
const SIGNATURE_STR: &'static str = "h";
const ALIGNMENT: usize = <u32>::ALIGNMENT;
fn alignment(format: EncodingFormat) -> usize {
u32::alignment(format)
}
}
impl io::AsRawFd for Fd {
fn as_raw_fd(&self) -> io::RawFd {
self.0
}
}
impl Type for Fd {
fn signature() -> Signature<'static> {
Signature::from_str_unchecked(Self::SIGNATURE_STR)
}
}
impl Serialize for Fd {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_i32(self.0)
}
}
impl<'de> Deserialize<'de> for Fd {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
Ok(Fd(i32::deserialize(deserializer)?))
}
}
impl From<io::RawFd> for Fd {
fn from(value: io::RawFd) -> Self {
Self(value)
}
}
impl<T> From<&T> for Fd
where
T: io::AsRawFd,
{
fn from(t: &T) -> Self {
Self(t.as_raw_fd())
}
}
impl std::fmt::Display for Fd {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}