1#[cfg(unix)]
2use crate::OwnedFd;
3use std::ops::Deref;
45use crate::serialized::Context;
67/// Represents the return value of [`crate::to_writer`] function.
8///
9/// It mainly contains the size of serialized bytes in a specific format.
10///
11/// On Unix platforms, it also contains a list of file descriptors, whose indexes are included in
12/// the serialized bytes.
13#[derive(Debug)]
14pub struct Written {
15 size: usize,
16 context: Context,
17#[cfg(unix)]
18fds: Vec<OwnedFd>,
19}
2021impl Written {
22/// Create a new `Written` instance.
23pub fn new(size: usize, context: Context) -> Self {
24Self {
25 size,
26 context,
27#[cfg(unix)]
28fds: vec![],
29 }
30 }
3132/// Set the file descriptors.
33#[cfg(unix)]
34pub fn set_fds(mut self, fds: impl IntoIterator<Item = impl Into<OwnedFd>>) -> Self {
35self.fds = fds.into_iter().map(Into::into).collect();
36self
37}
3839/// The size of the serialized bytes.
40pub fn size(&self) -> usize {
41self.size
42 }
4344/// The encoding context.
45pub fn context(&self) -> Context {
46self.context
47 }
4849/// Consume `self` and return the file descriptors.
50 ///
51 /// This method is only available on Unix platforms.
52#[cfg(unix)]
53pub fn into_fds(self) -> Vec<OwnedFd> {
54self.fds
55 }
5657/// The file descriptors that are references by the serialized bytes.
58 ///
59 /// This method is only available on Unix platforms.
60#[cfg(unix)]
61pub fn fds(&self) -> &[OwnedFd] {
62&self.fds
63 }
64}
6566impl Deref for Written {
67type Target = usize;
6869fn deref(&self) -> &Self::Target {
70&self.size
71 }
72}