Skip to main content

dope_core/io/socket/addr/prepared/
mod.rs

1use std::{net, os::fd};
2
3use dope_sys::{Errno, io::socket::addr};
4
5mod sealed;
6
7pub(crate) use sealed::Sealed;
8
9use crate::io::socket::{self, StreamSpec};
10
11/// Validated, byte-stable storage for a retained socket operation.
12pub trait Prepared: Sealed + Copy {
13    fn view(&self) -> View<'_>;
14
15    fn stream_spec(&self) -> StreamSpec;
16}
17
18/// A validated address borrowed for one socket operation.
19pub struct View<'a>(pub(crate) addr::Addr<'a>);
20
21impl<'a> View<'a> {
22    /// Reads the local address into the caller's storage.
23    pub fn local(
24        fd: fd::BorrowedFd<'_>,
25        storage: &'a mut addr::Storage,
26    ) -> Result<Self, addr::Local<Errno>> {
27        addr::Addr::local::<crate::Backend, Errno>(fd, storage).map(Self)
28    }
29
30    pub fn into_std(self) -> Result<net::SocketAddr, socket::Error> {
31        self.0.into_std().map_err(socket::Error::from)
32    }
33
34    pub(crate) fn native(self) -> addr::Addr<'a> {
35        self.0
36    }
37}