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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
pub(crate) mod primitive;
pub(crate) mod variable;

pub use primitive::Array as PrimitiveArray;
pub use primitive::Builder as PrimitiveBuilder;
pub use variable::{BinaryArray, BinaryArrayIter, BinaryBuilder};
pub use variable::{StringArray, StringArrayIter, StringBuilder};

use crate::datatypes::*;
use crate::memory::Buffer;
use std::any::Any;
use std::fmt;
use std::sync::Arc;

/// A dynamically-typed array.
pub trait Array: fmt::Debug + Send + Sync {
    fn as_any(&self) -> &dyn Any;
    #[must_use]
    fn is_empty(&self) -> bool {
        self.len() == 0
    }
    /// Returns the number of elements of this array.
    fn len(&self) -> usize;
    fn data(&self) -> &Data;
}

/// An `Array` builder.
pub trait Builder {
    /// Returns the number of array slots in the builder.
    fn len(&self) -> usize;
    /// Converts inself into an `Array`.
    fn build(self) -> Arc<dyn Array>;
}

/// A generic representation of array data.
#[derive(PartialEq, Debug, Clone)]
pub struct Data {
    /// The element type for this array data.
    data_type: DataType,
    /// The number of elements in this array data.
    len: usize,
    buffers: Vec<Buffer>,
}

impl Data {
    pub fn data_type(&self) -> &DataType {
        &self.data_type
    }
}

struct RawPtrBox<T> {
    inner: *const T,
}

impl<T> RawPtrBox<T> {
    fn new(inner: *const T) -> Self {
        Self { inner }
    }

    fn get(&self) -> *const T {
        self.inner
    }
}

unsafe impl<T> Send for RawPtrBox<T> {}
unsafe impl<T> Sync for RawPtrBox<T> {}