zvariant/serialized/
size.rs

1use std::ops::Deref;
2
3use crate::serialized::Context;
4
5/// Represents the return value of [`crate::serialized_size`] function.
6///
7/// It mainly contains the size of serialized bytes in a specific format.
8///
9/// On Unix platforms, it also contains the number of file descriptors, whose indexes are included
10/// in the serialized bytes.
11#[derive(Debug)]
12pub struct Size {
13    size: usize,
14    context: Context,
15    #[cfg(unix)]
16    num_fds: u32,
17}
18
19impl Size {
20    /// Create a new `Size` instance.
21    pub fn new(size: usize, context: Context) -> Self {
22        Self {
23            size,
24            context,
25            #[cfg(unix)]
26            num_fds: 0,
27        }
28    }
29
30    /// Set the number of file descriptors.
31    #[cfg(unix)]
32    pub fn set_num_fds(mut self, num_fds: u32) -> Self {
33        self.num_fds = num_fds;
34        self
35    }
36
37    /// The size of the serialized bytes.
38    pub fn size(&self) -> usize {
39        self.size
40    }
41
42    /// The encoding context.
43    pub fn context(&self) -> Context {
44        self.context
45    }
46
47    /// The number file descriptors that are references by the serialized bytes.
48    ///
49    /// This method is only available on Unix platforms.
50    #[cfg(unix)]
51    pub fn num_fds(&self) -> u32 {
52        self.num_fds
53    }
54}
55
56impl Deref for Size {
57    type Target = usize;
58
59    fn deref(&self) -> &Self::Target {
60        &self.size
61    }
62}