Skip to content

Commit 597d088

Browse files
authored
Merge pull request #1317 from vardbabayan/vb/1003-add-maybeunset-fromoption
feat: add MaybeUnset::from_option() to reduce boilerplate
2 parents dea72ee + d85b09a commit 597d088

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

Diff for: scylla-cql/src/serialize/value_tests.rs

+18
Original file line numberDiff line numberDiff line change
@@ -2349,6 +2349,24 @@ fn unset_value() {
23492349
do_serialize(set_i32, &ColumnType::Native(NativeType::Int)),
23502350
vec![0, 0, 0, 4, 0, 0, 0, 32]
23512351
);
2352+
2353+
let unset_option_i32: Option<i32> = None;
2354+
assert_eq!(
2355+
do_serialize(
2356+
MaybeUnset::from_option(unset_option_i32),
2357+
&ColumnType::Native(NativeType::Int)
2358+
),
2359+
&(-2_i32).to_be_bytes()[..]
2360+
);
2361+
2362+
let set_option_i32: Option<i32> = Some(44);
2363+
assert_eq!(
2364+
do_serialize(
2365+
MaybeUnset::from_option(set_option_i32),
2366+
&ColumnType::Native(NativeType::Int)
2367+
),
2368+
vec![0, 0, 0, 4, 0, 0, 0, 44]
2369+
);
23522370
}
23532371

23542372
#[test]

Diff for: scylla-cql/src/value.rs

+10
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ pub enum MaybeUnset<V> {
3333
Set(V),
3434
}
3535

36+
impl<V> MaybeUnset<V> {
37+
#[inline]
38+
pub fn from_option(opt: Option<V>) -> Self {
39+
match opt {
40+
Some(v) => Self::Set(v),
41+
None => Self::Unset,
42+
}
43+
}
44+
}
45+
3646
/// Represents timeuuid (uuid V1) value
3747
///
3848
/// This type has custom comparison logic which follows Scylla/Cassandra semantics.

0 commit comments

Comments
 (0)