Skip to content

Commit

Permalink
try_new
Browse files Browse the repository at this point in the history
  • Loading branch information
gatesn committed Mar 26, 2024
1 parent b598666 commit c343852
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 17 deletions.
2 changes: 1 addition & 1 deletion vortex-array/src/array/bool/compute/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl FlattenFn for BoolArray {

impl ScalarAtFn for BoolArray {
fn scalar_at(&self, index: usize) -> VortexResult<Scalar> {
Ok(BoolScalar::new(
Ok(BoolScalar::try_new(
self.is_valid(index).then(|| self.buffer.value(index)),
self.nullability(),
)
Expand Down
2 changes: 1 addition & 1 deletion vortex-array/src/array/primitive/compute/scalar_at.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::validity::ArrayValidity;
impl ScalarAtFn for PrimitiveArray {
fn scalar_at(&self, index: usize) -> VortexResult<Scalar> {
match_each_native_ptype!(self.ptype, |$T| {
Ok(PrimitiveScalar::new(
Ok(PrimitiveScalar::try_new(
self.is_valid(index).then(|| self.typed_data::<$T>()[index]),
self.nullability(),
)?.into())
Expand Down
13 changes: 8 additions & 5 deletions vortex-array/src/scalar/primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ pub struct PrimitiveScalar {
}

impl PrimitiveScalar {
pub fn new<T: NativePType>(value: Option<T>, nullability: Nullability) -> VortexResult<Self> {
pub fn try_new<T: NativePType>(
value: Option<T>,
nullability: Nullability,
) -> VortexResult<Self> {
if value.is_none() && nullability == Nullability::NonNullable {
return Err("Value cannot be None for NonNullable Scalar".into());
}
Expand All @@ -32,15 +35,15 @@ impl PrimitiveScalar {
}

pub fn nullable<T: NativePType>(value: Option<T>) -> Self {
Self::new(value, Nullability::Nullable).unwrap()
Self::try_new(value, Nullability::Nullable).unwrap()
}

pub fn some<T: NativePType>(value: T) -> Self {
Self::new::<T>(Some(value), Nullability::default()).unwrap()
Self::try_new::<T>(Some(value), Nullability::default()).unwrap()
}

pub fn none<T: NativePType>() -> Self {
Self::new::<T>(None, Nullability::Nullable).unwrap()
Self::try_new::<T>(None, Nullability::Nullable).unwrap()
}

#[inline]
Expand All @@ -61,7 +64,7 @@ impl PrimitiveScalar {
pub fn cast(&self, dtype: &DType) -> VortexResult<Scalar> {
let ptype: PType = dtype.try_into()?;
match_each_native_ptype!(ptype, |$T| {
Ok(PrimitiveScalar::new(
Ok(PrimitiveScalar::try_new(
self.value()
.map(|ps| ps.cast_ptype(ptype))
.transpose()?
Expand Down
8 changes: 4 additions & 4 deletions vortex-array/src/scalar/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ impl<'a, 'b> ScalarReader<'a, 'b> {
match tag {
ScalarTag::Binary => {
let slice = self.reader.read_optional_slice()?;
Ok(BinaryScalar::new(slice, nullability)?.into())
Ok(BinaryScalar::try_new(slice, nullability)?.into())
}
ScalarTag::Bool => {
let is_present = self.reader.read_option_tag()?;
let bool = self.reader.read_nbytes::<1>()?[0] != 0;
Ok(BoolScalar::new(is_present.then_some(bool), nullability)?.into())
Ok(BoolScalar::try_new(is_present.then_some(bool), nullability)?.into())
}
ScalarTag::PrimitiveS => self.read_primitive_scalar(nullability).map(|p| p.into()),
ScalarTag::List => {
Expand Down Expand Up @@ -72,7 +72,7 @@ impl<'a, 'b> ScalarReader<'a, 'b> {
}
ScalarTag::Utf8 => {
let value = self.reader.read_optional_slice()?;
Ok(Utf8Scalar::new(
Ok(Utf8Scalar::try_new(
value.map(|v| unsafe { String::from_utf8_unchecked(v) }),
nullability,
)?
Expand All @@ -95,7 +95,7 @@ impl<'a, 'b> ScalarReader<'a, 'b> {
} else {
None
};
Ok(PrimitiveScalar::new::<$P>(value, nullability)?)
Ok(PrimitiveScalar::try_new::<$P>(value, nullability)?)
})
}
}
Expand Down
10 changes: 5 additions & 5 deletions vortex-array/src/scalar/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,27 @@ pub struct ScalarValue<T> {
}

impl<T> ScalarValue<T> {
pub fn new(value: Option<T>, nullability: Nullability) -> VortexResult<Self> {
pub fn try_new(value: Option<T>, nullability: Nullability) -> VortexResult<Self> {
if value.is_none() && nullability == Nullability::NonNullable {
return Err("Value cannot be None for NonNullable Scalar".into());
}
Ok(Self { value, nullability })
}

pub fn non_nullable(value: T) -> Self {
Self::new(Some(value), Nullability::NonNullable).unwrap()
Self::try_new(Some(value), Nullability::NonNullable).unwrap()
}

pub fn nullable(value: T) -> Self {
Self::new(Some(value), Nullability::Nullable).unwrap()
Self::try_new(Some(value), Nullability::Nullable).unwrap()
}

pub fn some(value: T) -> Self {
Self::new(Some(value), Nullability::default()).unwrap()
Self::try_new(Some(value), Nullability::default()).unwrap()
}

pub fn none() -> Self {
Self::new(None, Nullability::Nullable).unwrap()
Self::try_new(None, Nullability::Nullable).unwrap()
}

pub fn value(&self) -> Option<&T> {
Expand Down
4 changes: 3 additions & 1 deletion vortex-dict/src/compress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,9 @@ mod test {
);
assert_eq!(
scalar_at(&values, 0),
Ok(PrimitiveScalar::new::<i32>(None, Nullable).unwrap().into())
Ok(PrimitiveScalar::try_new::<i32>(None, Nullable)
.unwrap()
.into())
);
assert_eq!(
scalar_at(&values, 1),
Expand Down

0 comments on commit c343852

Please sign in to comment.