|
4 | 4 | //! Conversions for [`PrimitiveScalar`]s. |
5 | 5 |
|
6 | 6 | use vortex_error::VortexError; |
7 | | -use vortex_error::VortexExpect; |
8 | 7 | use vortex_error::VortexResult; |
9 | 8 | use vortex_error::vortex_err; |
10 | 9 |
|
@@ -104,26 +103,29 @@ macro_rules! primitive_scalar { |
104 | 103 | /// Non-nullable `Into<Scalar>` implementation for T. |
105 | 104 | impl From<$T> for Scalar { |
106 | 105 | fn from(value: $T) -> Self { |
107 | | - Scalar::try_new( |
108 | | - DType::Primitive(<$T>::PTYPE, Nullability::NonNullable), |
109 | | - Some(ScalarValue::Primitive(value.into())), |
110 | | - ) |
111 | | - .vortex_expect( |
112 | | - "somehow unable to construct a primitive `Scalar` from a native type", |
113 | | - ) |
| 106 | + // SAFETY: Primitive dtype of T::PTYPE by definition always |
| 107 | + // matches Primitive value built from T |
| 108 | + unsafe { |
| 109 | + Scalar::new_unchecked( |
| 110 | + DType::Primitive(<$T>::PTYPE, Nullability::NonNullable), |
| 111 | + Some(ScalarValue::Primitive(value.into())), |
| 112 | + ) |
| 113 | + } |
114 | 114 | } |
115 | 115 | } |
116 | 116 |
|
117 | 117 | /// Nullable `Into<Scalar>` implementation for T. |
118 | 118 | impl From<Option<$T>> for Scalar { |
119 | 119 | fn from(value: Option<$T>) -> Self { |
120 | | - Scalar::try_new( |
121 | | - DType::Primitive(<$T>::PTYPE, Nullability::Nullable), |
122 | | - value.map(|value| ScalarValue::Primitive(value.into())), |
123 | | - ) |
124 | | - .vortex_expect( |
125 | | - "somehow unable to construct a primitive `Scalar` from a native type", |
126 | | - ) |
| 120 | + // SAFETY: Primitive dtype of T::PTYPE by definition always |
| 121 | + // matches Primitive value built from T. dtype is nullable so |
| 122 | + // None is valid |
| 123 | + unsafe { |
| 124 | + Scalar::new_unchecked( |
| 125 | + DType::Primitive(<$T>::PTYPE, Nullability::Nullable), |
| 126 | + value.map(|value| ScalarValue::Primitive(value.into())), |
| 127 | + ) |
| 128 | + } |
127 | 129 | } |
128 | 130 | } |
129 | 131 | }; |
@@ -199,20 +201,25 @@ impl From<usize> for ScalarValue { |
199 | 201 |
|
200 | 202 | impl From<usize> for Scalar { |
201 | 203 | fn from(value: usize) -> Self { |
202 | | - Scalar::try_new( |
203 | | - DType::Primitive(PType::U64, Nullability::NonNullable), |
204 | | - Some(ScalarValue::Primitive((value as u64).into())), |
205 | | - ) |
206 | | - .vortex_expect("somehow unable to construct a primitive `Scalar` from a native type") |
| 204 | + // SAFETY: U64 always matches a primitive value built from u64 |
| 205 | + unsafe { |
| 206 | + Scalar::new_unchecked( |
| 207 | + DType::Primitive(PType::U64, Nullability::NonNullable), |
| 208 | + Some(ScalarValue::Primitive((value as u64).into())), |
| 209 | + ) |
| 210 | + } |
207 | 211 | } |
208 | 212 | } |
209 | 213 |
|
210 | 214 | impl From<Option<usize>> for Scalar { |
211 | 215 | fn from(value: Option<usize>) -> Self { |
212 | | - Scalar::try_new( |
213 | | - DType::Primitive(PType::U64, Nullability::Nullable), |
214 | | - value.map(|value| ScalarValue::Primitive((value as u64).into())), |
215 | | - ) |
216 | | - .vortex_expect("somehow unable to construct a primitive `Scalar` from a native type") |
| 216 | + // SAFETY: U64 always matches a primitive value built from u64. Dtype |
| 217 | + // is nullable so None is valid. |
| 218 | + unsafe { |
| 219 | + Scalar::new_unchecked( |
| 220 | + DType::Primitive(PType::U64, Nullability::Nullable), |
| 221 | + value.map(|value| ScalarValue::Primitive((value as u64).into())), |
| 222 | + ) |
| 223 | + } |
217 | 224 | } |
218 | 225 | } |
0 commit comments