vortex_array/arrays/constant/
array.rs1use std::fmt::Display;
5use std::fmt::Formatter;
6
7use crate::ArraySlots;
8use crate::array::Array;
9use crate::array::ArrayParts;
10use crate::arrays::Constant;
11use crate::scalar::Scalar;
12
13#[derive(Clone, Debug)]
14pub struct ConstantData {
15 pub(super) scalar: Scalar,
16}
17
18impl Display for ConstantData {
19 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
20 write!(f, "scalar: {}", self.scalar)
21 }
22}
23
24impl ConstantData {
25 pub fn new<S>(scalar: S) -> Self
26 where
27 S: Into<Scalar>,
28 {
29 let scalar = scalar.into();
30 Self { scalar }
31 }
32
33 pub fn scalar(&self) -> &Scalar {
35 &self.scalar
36 }
37
38 pub fn into_parts(self) -> Scalar {
39 self.scalar
40 }
41}
42
43impl Array<Constant> {
44 pub fn new<S>(scalar: S, len: usize) -> Self
45 where
46 S: Into<Scalar>,
47 {
48 let scalar = scalar.into();
49 let dtype = scalar.dtype().clone();
50 let data = ConstantData::new(scalar);
51 unsafe {
52 Array::from_parts_unchecked(ArrayParts::new(
53 Constant,
54 dtype,
55 len,
56 data,
57 ArraySlots::new(),
58 ))
59 }
60 }
61}