Skip to content

Commit

Permalink
Add serialization traits
Browse files Browse the repository at this point in the history
This commit adds experimental version of new serialization traits.
Blanket implementation are provided to support older traits.
  • Loading branch information
Lorak-mmk committed Sep 28, 2023
1 parent 07b262c commit 95d11bc
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 0 deletions.
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;
2 changes: 2 additions & 0 deletions scylla-cql/src/types/serialize/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod serialize_row;
pub mod serialize_value;
44 changes: 44 additions & 0 deletions scylla-cql/src/types/serialize/serialize_row.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use std::{any::Any, sync::Arc};

use crate::{_macro_internal::ValueList, frame::response::result::ColumnSpec};

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

impl<'a> RowSerializationContext<'a> {
pub fn column_by_idx(&self, i: usize) -> Option<&ColumnSpec> {
self.columns.get(i)
}

// TODO: change RowSerializationContext to make this faster
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(&self, ctx: &RowSerializationContext<'_>) -> bool;
fn serialize(
&self,
ctx: &RowSerializationContext<'_>,
out: &mut Vec<u8>,
) -> Result<(), Arc<dyn Any + Send + Sync>>;
}

impl<T: ValueList> SerializeRow for T {
fn preliminary_type_check(&self, _ctx: &RowSerializationContext<'_>) -> bool {
true
}

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

use crate::{_macro_internal::Value, frame::response::result::ColumnType};

pub trait SerializeCql {
fn preliminary_type_check(&self, typ: &ColumnType) -> bool;
fn serialize(
&self,
typ: &ColumnType,
buf: &mut Vec<u8>,
) -> Result<(), Arc<dyn Any + Send + Sync>>;
}

impl<T: Value> SerializeCql for T {
fn preliminary_type_check(&self, _typ: &ColumnType) -> bool {
true
}

fn serialize(
&self,
_typ: &ColumnType,
buf: &mut Vec<u8>,
) -> Result<(), Arc<dyn Any + Send + Sync>> {
self.serialize(buf).map_err(|err| {
let err: Arc<dyn Any + Send + Sync> = Arc::new(err);
err
})
}
}

0 comments on commit 95d11bc

Please sign in to comment.