Skip to content

Commit 0570dd7

Browse files
committed
TryFrom<CassCollection> for CassCqlValue -> From
After introducing CollectionType and ensuring type safety for collections, we can simplify the conversion from `&CassCollection` to `CassCqlValue` by removing the `TryFrom` trait and using `From` instead. This change reflects that the conversion is always valid as long as the collection is well-formed.
1 parent 3bf4b26 commit 0570dd7

File tree

2 files changed

+9
-13
lines changed

2 files changed

+9
-13
lines changed

scylla-rust-wrapper/src/binding.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -319,10 +319,7 @@ macro_rules! invoke_binder_maker_macro_with_type {
319319
$consume_v,
320320
$fn,
321321
|p: CassBorrowedSharedPtr<crate::collection::CassCollection, CConst>| {
322-
match std::convert::TryInto::try_into(BoxFFI::as_ref(p).unwrap()) {
323-
Ok(v) => Ok(Some(v)),
324-
Err(_) => Err(CassError::CASS_ERROR_LIB_INVALID_VALUE_TYPE),
325-
}
322+
Ok(Some(std::convert::Into::into(BoxFFI::as_ref(p).unwrap())))
326323
},
327324
[p @ CassBorrowedSharedPtr<crate::collection::CassCollection, CConst>]
328325
);

scylla-rust-wrapper/src/collection.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -132,16 +132,15 @@ impl CassCollection {
132132
}
133133
}
134134

135-
impl TryFrom<&CassCollection> for CassCqlValue {
136-
type Error = ();
137-
fn try_from(collection: &CassCollection) -> Result<Self, Self::Error> {
135+
impl From<&CassCollection> for CassCqlValue {
136+
fn from(collection: &CassCollection) -> Self {
138137
// FIXME: validate that collection items are correct
139138
let data_type = collection.data_type.clone();
140139
match collection.collection_type {
141-
CollectionType::List => Ok(CassCqlValue::List {
140+
CollectionType::List => CassCqlValue::List {
142141
data_type,
143142
values: collection.items.clone(),
144-
}),
143+
},
145144
CollectionType::Map => {
146145
let mut grouped_items = Vec::new();
147146
// FIXME: validate even number of items
@@ -152,15 +151,15 @@ impl TryFrom<&CassCollection> for CassCqlValue {
152151
grouped_items.push((key, value));
153152
}
154153

155-
Ok(CassCqlValue::Map {
154+
CassCqlValue::Map {
156155
data_type,
157156
values: grouped_items,
158-
})
157+
}
159158
}
160-
CollectionType::Set => Ok(CassCqlValue::Set {
159+
CollectionType::Set => CassCqlValue::Set {
161160
data_type,
162161
values: collection.items.clone(),
163-
}),
162+
},
164163
}
165164
}
166165
}

0 commit comments

Comments
 (0)