1#[cfg(unix)]
2use crate::OwnedFd;
3use std::ops::Deref;
4
5use crate::serialized::Context;
6
7/// 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)]
18 fds: Vec<OwnedFd>,
19}
20
21impl Written {
22 /// Create a new `Written` instance.
23 pub fn new(size: usize, context: Context) -> Self {
24 Self {
25 size,
26 context,
27 #[cfg(unix)]
28 fds: vec![],
29 }
30 }
31
32 /// Set the file descriptors.
33 #[cfg(unix)]
34 pub fn set_fds(mut self, fds: impl IntoIterator<Item = impl Into<OwnedFd>>) -> Self {
35 self.fds = fds.into_iter().map(Into::into).collect();
36 self
37 }
38
39 /// The size of the serialized bytes.
40 pub fn size(&self) -> usize {
41 self.size
42 }
43
44 /// The encoding context.
45 pub fn context(&self) -> Context {
46 self.context
47 }
48
49 /// Consume `self` and return the file descriptors.
50 ///
51 /// This method is only available on Unix platforms.
52 #[cfg(unix)]
53 pub fn into_fds(self) -> Vec<OwnedFd> {
54 self.fds
55 }
56
57 /// The file descriptors that are references by the serialized bytes.
58 ///
59 /// This method is only available on Unix platforms.
60 #[cfg(unix)]
61 pub fn fds(&self) -> &[OwnedFd] {
62 &self.fds
63 }
64}
65
66impl Deref for Written {
67 type Target = usize;
68
69 fn deref(&self) -> &Self::Target {
70 &self.size
71 }
72}
73