-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit adds experimental version of new serialization traits. Blanket implementation are provided to support older traits.
- Loading branch information
Showing
5 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod serialize; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] { | ||
self.columns | ||
} | ||
|
||
// TODO: change RowSerializationContext to make this faster | ||
#[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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |