1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
use vortex_buffer::Buffer;
use vortex_error::{VortexExpect, VortexResult};

use crate::visitor::ArrayVisitor;
use crate::ArrayData;

impl ArrayData {
    /// Total size of the array in bytes, including all children and buffers.
    pub fn nbytes(&self) -> usize {
        let mut visitor = NBytesVisitor(0);
        self.encoding()
            .accept(self.as_ref(), &mut visitor)
            .vortex_expect("Failed to get nbytes from Array");
        visitor.0 + size_of_val(self.array_metadata())
    }
}

pub trait ArrayNBytes {
    /// Total size of the array in bytes, including all children and buffers.
    fn nbytes(&self) -> usize;
}

// Implement ArrayNBytes for all concrete arrays.
impl<A: AsRef<ArrayData>> ArrayNBytes for A {
    #[inline(always)]
    fn nbytes(&self) -> usize {
        self.as_ref().nbytes()
    }
}

struct NBytesVisitor(usize);

impl ArrayVisitor for NBytesVisitor {
    fn visit_child(&mut self, _name: &str, array: &ArrayData) -> VortexResult<()> {
        self.0 += array.nbytes();
        Ok(())
    }

    fn visit_buffer(&mut self, buffer: &Buffer) -> VortexResult<()> {
        self.0 += buffer.len();
        Ok(())
    }
}