Skip to main content

vortex_array/display/extractors/
nbytes.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use std::fmt;
5
6use humansize::DECIMAL;
7use humansize::format_size;
8
9use crate::ArrayRef;
10use crate::display::extractor::TreeContext;
11use crate::display::extractor::TreeExtractor;
12
13/// Extractor that adds `nbytes=X (Y%)` to the header line.
14pub struct NbytesExtractor;
15
16impl TreeExtractor for NbytesExtractor {
17    fn write_header(
18        &self,
19        array: &ArrayRef,
20        ctx: &TreeContext,
21        f: &mut fmt::Formatter<'_>,
22    ) -> fmt::Result {
23        let nbytes = array.nbytes();
24        let total_size = ctx.parent_total_size().unwrap_or(nbytes);
25        let percent = if total_size == 0 {
26            0.0
27        } else {
28            100_f64 * nbytes as f64 / total_size as f64
29        };
30        write!(
31            f,
32            " nbytes={} ({:.2}%)",
33            format_size(nbytes, DECIMAL),
34            percent
35        )
36    }
37}