-
Notifications
You must be signed in to change notification settings - Fork 110
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod serialize; |
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>; |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: what do you mean by this comment? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
} | ||
} |
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) | ||
} | ||
} |
There was a problem hiding this comment.
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.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done