Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add serialization traits #819

Merged
merged 1 commit into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions scylla-cql/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ pub mod frame;
#[macro_use]
pub mod macros;

pub mod types;

pub use crate::frame::response::cql_to_rust;
pub use crate::frame::response::cql_to_rust::FromRow;

Expand Down
1 change: 1 addition & 0 deletions scylla-cql/src/types/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod serialize;
6 changes: 6 additions & 0 deletions scylla-cql/src/types/serialize/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use std::{any::Any, sync::Arc};

pub mod row;
pub mod value;

type SerializationError = Arc<dyn Any + Send + Sync>;
49 changes: 49 additions & 0 deletions scylla-cql/src/types/serialize/row.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use std::sync::Arc;

use crate::frame::response::result::ColumnSpec;
use crate::frame::value::ValueList;

use super::SerializationError;

pub struct RowSerializationContext<'a> {
columns: &'a [ColumnSpec],
}

impl<'a> RowSerializationContext<'a> {
#[inline]
pub fn columns(&self) -> &'a [ColumnSpec] {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: this method and column_by_name could be made #[inline] - they are small and simple, and without it the compiler won't consider inlining them in when used in external crates.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

self.columns
}

// TODO: change RowSerializationContext to make this faster
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: what do you mean by this comment?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apart from list of clumns we can also keep a hashmap from name to column spec so that name lookups are in constant time instead of linear.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see.

#[inline]
pub fn column_by_name(&self, target: &str) -> Option<&ColumnSpec> {
self.columns.iter().find(|&c| c.name == target)
}
}

pub trait SerializeRow {
fn preliminary_type_check(ctx: &RowSerializationContext<'_>) -> Result<(), SerializationError>;
fn serialize(
&self,
ctx: &RowSerializationContext<'_>,
out: &mut Vec<u8>,
) -> Result<(), SerializationError>;
}

impl<T: ValueList> SerializeRow for T {
fn preliminary_type_check(
_ctx: &RowSerializationContext<'_>,
) -> Result<(), SerializationError> {
Ok(())
}

fn serialize(
&self,
_ctx: &RowSerializationContext<'_>,
out: &mut Vec<u8>,
) -> Result<(), SerializationError> {
self.write_to_request(out)
.map_err(|err| Arc::new(err) as SerializationError)
}
}
22 changes: 22 additions & 0 deletions scylla-cql/src/types/serialize/value.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use std::sync::Arc;

use crate::frame::response::result::ColumnType;
use crate::frame::value::Value;

use super::SerializationError;

pub trait SerializeCql {
fn preliminary_type_check(typ: &ColumnType) -> Result<(), SerializationError>;
fn serialize(&self, typ: &ColumnType, buf: &mut Vec<u8>) -> Result<(), SerializationError>;
}

impl<T: Value> SerializeCql for T {
fn preliminary_type_check(_typ: &ColumnType) -> Result<(), SerializationError> {
Ok(())
}

fn serialize(&self, _typ: &ColumnType, buf: &mut Vec<u8>) -> Result<(), SerializationError> {
self.serialize(buf)
.map_err(|err| Arc::new(err) as SerializationError)
}
}