Skip to content

Commit

Permalink
codewide: introduce DeserializeOwned{Row,Value}
Browse files Browse the repository at this point in the history
As noted in a review comment, parsing `for<'r> DeserializeValue<'r, 'r>`
to understand it's requiring an owned type is nontrivial and could be
replaced with a subtrait with an informative name.
Therefore, this commit introduces DeserializeOwnedRow and
DeserializeOwnedValue (to be used by the `scylla` crate itself only).
  • Loading branch information
wprzytula committed Nov 10, 2024
1 parent e53b7a8 commit 8384730
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 10 deletions.
7 changes: 7 additions & 0 deletions scylla/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,13 @@ pub mod deserialize {
UdtIterator, UdtTypeCheckErrorKind,
};
}

// Shorthands for better readability.
#[cfg_attr(not(test), allow(unused))]
pub(crate) trait DeserializeOwnedValue: for<'r> DeserializeValue<'r, 'r> {}
impl<T> DeserializeOwnedValue for T where T: for<'r> DeserializeValue<'r, 'r> {}
pub(crate) trait DeserializeOwnedRow: for<'r> DeserializeRow<'r, 'r> {}
impl<T> DeserializeOwnedRow for T where T: for<'r> DeserializeRow<'r, 'r> {}
}

pub mod authentication;
Expand Down
4 changes: 2 additions & 2 deletions scylla/src/transport/cql_collections_test.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::deserialize::DeserializeOwnedValue;
use crate::transport::session::Session;
use scylla_cql::types::deserialize::value::DeserializeValue;

use crate::frame::response::result::CqlValue;
use crate::test_utils::{create_new_session_builder, setup_tracing};
Expand Down Expand Up @@ -36,7 +36,7 @@ async fn insert_and_select<InsertT, SelectT>(
expected: &SelectT,
) where
InsertT: SerializeValue,
SelectT: for<'r> DeserializeValue<'r, 'r> + PartialEq + std::fmt::Debug,
SelectT: DeserializeOwnedValue + PartialEq + std::fmt::Debug,
{
session
.query_unpaged(
Expand Down
6 changes: 3 additions & 3 deletions scylla/src/transport/cql_types_test.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate as scylla;
use crate::deserialize::DeserializeOwnedValue;
use crate::frame::response::result::CqlValue;
use crate::frame::value::{Counter, CqlDate, CqlTime, CqlTimestamp};
use crate::test_utils::{create_new_session_builder, scylla_supports_tablets, setup_tracing};
use crate::transport::session::Session;
use crate::utils::test_utils::unique_keyspace_name;
use itertools::Itertools;
use scylla_cql::frame::value::{CqlTimeuuid, CqlVarint};
use scylla_cql::types::deserialize::value::DeserializeValue;
use scylla_cql::types::serialize::value::SerializeValue;
use scylla_macros::{DeserializeValue, SerializeValue};
use std::cmp::PartialEq;
Expand Down Expand Up @@ -74,7 +74,7 @@ async fn init_test(table_name: &str, type_name: &str) -> Session {
// Expected values and bound values are computed using T::from_str
async fn run_tests<T>(tests: &[&str], type_name: &str)
where
T: SerializeValue + for<'r> DeserializeValue<'r, 'r> + FromStr + Debug + Clone + PartialEq,
T: SerializeValue + DeserializeOwnedValue + FromStr + Debug + Clone + PartialEq,
{
let session: Session = init_test(type_name, type_name).await;
session.await_schema_agreement().await.unwrap();
Expand Down Expand Up @@ -1799,7 +1799,7 @@ async fn test_udt_with_missing_field() {
expected: TR,
) where
TQ: SerializeValue,
TR: for<'r> DeserializeValue<'r, 'r> + PartialEq + Debug,
TR: DeserializeOwnedValue + PartialEq + Debug,
{
session
.query_unpaged(
Expand Down
3 changes: 2 additions & 1 deletion scylla/src/transport/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use super::query_result::ColumnSpecs;
use super::session::RequestSpan;
use crate::cql_to_rust::{FromRow, FromRowError};

use crate::deserialize::DeserializeOwnedRow;
use crate::frame::response::{
result,
result::{ColumnSpec, Row},
Expand Down Expand Up @@ -1076,7 +1077,7 @@ impl<RowT> TypedRowStream<RowT> {
/// It only works with owned types! For example, &str is not supported.
impl<RowT> Stream for TypedRowStream<RowT>
where
RowT: for<'r> DeserializeRow<'r, 'r>,
RowT: DeserializeOwnedRow,
{
type Item = Result<RowT, QueryError>;

Expand Down
4 changes: 2 additions & 2 deletions scylla/src/transport/session_test.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::batch::{Batch, BatchStatement};
use crate::deserialize::DeserializeValue;
use crate::deserialize::DeserializeOwnedValue;
use crate::prepared_statement::PreparedStatement;
use crate::query::Query;
use crate::retry_policy::{QueryInfo, RetryDecision, RetryPolicy, RetrySession};
Expand Down Expand Up @@ -3100,7 +3100,7 @@ async fn test_deserialize_empty_collections() {
session.use_keyspace(&ks, true).await.unwrap();

async fn deserialize_empty_collection<
Collection: Default + for<'frame> DeserializeValue<'frame, 'frame> + SerializeValue,
Collection: Default + DeserializeOwnedValue + SerializeValue,
>(
session: &Session,
collection_name: &str,
Expand Down
4 changes: 2 additions & 2 deletions scylla/src/transport/topology.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::deserialize::DeserializeOwnedRow;
use crate::frame::response::event::Event;
use crate::routing::Token;
use crate::statement::query::Query;
Expand All @@ -15,7 +16,6 @@ use futures::Stream;
use rand::seq::SliceRandom;
use rand::{thread_rng, Rng};
use scylla_cql::frame::frame_errors::RowsParseError;
use scylla_cql::types::deserialize::row::DeserializeRow;
use scylla_cql::types::deserialize::TypeCheckError;
use scylla_macros::DeserializeRow;
use std::borrow::BorrowMut;
Expand Down Expand Up @@ -930,7 +930,7 @@ fn query_filter_keyspace_name<'a, R>(
convert_typecheck_error: impl FnOnce(TypeCheckError) -> MetadataError + 'a,
) -> impl Stream<Item = Result<R, QueryError>> + 'a
where
R: for<'r> DeserializeRow<'r, 'r> + 'static,
R: DeserializeOwnedRow + 'static,
{
let conn = conn.clone();

Expand Down

0 comments on commit 8384730

Please sign in to comment.