membacking/mapping_manager/
mappable.rs1use mesh::payload::DefaultEncoding;
5use mesh::payload::encoding::ResourceField;
6use std::sync::Arc;
7
8cfg_select! {
9 windows => {
10 type OwnedType = std::os::windows::io::OwnedHandle;
12 }
13 unix => {
14 type OwnedType = std::os::fd::OwnedFd;
16 }
17}
18
19#[derive(Debug, Clone)]
23pub struct Mappable(Arc<OwnedType>);
24
25impl From<OwnedType> for Mappable {
26 fn from(value: OwnedType) -> Self {
27 Self(Arc::new(value))
28 }
29}
30
31impl From<Arc<OwnedType>> for Mappable {
32 fn from(value: Arc<OwnedType>) -> Self {
33 Self(value)
34 }
35}
36
37impl From<Mappable> for OwnedType {
38 fn from(value: Mappable) -> Self {
39 Arc::try_unwrap(value.0).unwrap_or_else(|v| v.try_clone().expect("out of fds/handles"))
42 }
43}
44
45#[cfg(unix)]
46impl std::os::fd::AsFd for Mappable {
47 fn as_fd(&self) -> std::os::fd::BorrowedFd<'_> {
48 self.0.as_fd()
49 }
50}
51
52#[cfg(windows)]
53impl std::os::windows::io::AsHandle for Mappable {
54 fn as_handle(&self) -> std::os::windows::io::BorrowedHandle<'_> {
55 self.0.as_handle()
56 }
57}
58
59impl Mappable {
60 pub fn inner_arc(&self) -> Arc<OwnedType> {
62 self.0.clone()
63 }
64}
65
66impl DefaultEncoding for Mappable {
67 type Encoding = ResourceField<OwnedType>;
68}