Skip to main content

fits_io/bin_table/
owned_row.rs

1use crate::bin_table::{FieldDefinition, Row, Value};
2use std::sync::Arc;
3
4/// A binary table row that owns the bytes it decodes from.
5///
6/// [`Row`] borrows both its bytes and its column definitions, which ties it to a
7/// [`BinTable`](crate::bin_table::BinTable) held in memory. A streamed row has
8/// no such table behind it — not holding one is the point of streaming — so it
9/// keeps its own copy of the row and shares one set of column definitions with
10/// the rest of the stream.
11#[derive(Debug, Clone)]
12pub struct OwnedRow {
13    data: Vec<u8>,
14    heap: Arc<Vec<u8>>,
15    field_definitions: Arc<Vec<FieldDefinition>>,
16}
17
18impl OwnedRow {
19    pub(crate) fn new(
20        field_definitions: Arc<Vec<FieldDefinition>>,
21        heap: Arc<Vec<u8>>,
22        data: Vec<u8>,
23    ) -> Self {
24        Self {
25            data,
26            heap,
27            field_definitions,
28        }
29    }
30
31    /// Borrows this row, for the parts of the API that take a [`Row`].
32    pub fn row(&self) -> Row<'_> {
33        Row::new(&self.field_definitions, &self.data, &self.heap)
34    }
35
36    /// Reads the column named `key`, or `None` if this row has no such column.
37    pub fn get(&self, key: &str) -> crate::Result<Option<Value>> {
38        self.row().get(key)
39    }
40
41    /// Reads the column at `index`, or `None` if this row has no such column.
42    pub fn get_at(&self, index: usize) -> crate::Result<Option<Value>> {
43        self.row().get_at(index)
44    }
45
46    /// The columns this row is made of.
47    pub fn field_definitions(&self) -> &[FieldDefinition] {
48        &self.field_definitions
49    }
50}