-
Notifications
You must be signed in to change notification settings - Fork 46
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
feat: impl DataType::Float32/64
#240
Open
KKould
wants to merge
1
commit into
main
Choose a base branch
from
feat/float
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -3,6 +3,7 @@ use std::ops::Bound; | |
use bytes::Bytes; | ||
use fusio::path::Path; | ||
use futures_util::stream::StreamExt; | ||
use ordered_float::OrderedFloat; | ||
use tokio::fs; | ||
use tonbo::{executor::tokio::TokioExecutor, DbOption, Projection, Record, DB}; | ||
|
||
|
@@ -15,6 +16,7 @@ pub struct User { | |
email: Option<String>, | ||
age: u8, | ||
bytes: Bytes, | ||
float: Option<OrderedFloat<f32>>, | ||
} | ||
|
||
#[tokio::main] | ||
|
@@ -32,6 +34,7 @@ async fn main() { | |
email: Some("[email protected]".into()), | ||
age: 22, | ||
bytes: Bytes::from(vec![0, 1, 2]), | ||
float: Some(OrderedFloat(1.1)), | ||
}) | ||
.await | ||
.unwrap(); | ||
|
@@ -61,7 +64,7 @@ async fn main() { | |
let mut scan = txn | ||
.scan((Bound::Included(&name), Bound::Excluded(&upper))) | ||
// tonbo supports pushing down projection | ||
.projection(vec![1, 3]) | ||
.projection(vec![1, 3, 4]) | ||
// push down limitation | ||
.limit(1) | ||
.take() | ||
|
@@ -75,6 +78,7 @@ async fn main() { | |
email: Some("[email protected]"), | ||
age: None, | ||
bytes: Some(&[0, 1, 2]), | ||
float: Some(OrderedFloat(1.1)), | ||
}) | ||
); | ||
} | ||
|
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
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,65 @@ | ||
use fusio::{SeqRead, Write}; | ||
use ordered_float::OrderedFloat; | ||
|
||
use crate::serdes::{Decode, Encode}; | ||
|
||
impl<T> Decode for OrderedFloat<T> | ||
where | ||
T: Decode + ordered_float::FloatCore, | ||
{ | ||
type Error = T::Error; | ||
|
||
async fn decode<R>(reader: &mut R) -> Result<Self, Self::Error> | ||
where | ||
R: SeqRead, | ||
{ | ||
Ok(OrderedFloat::from(T::decode(reader).await?)) | ||
} | ||
} | ||
|
||
impl<T> Encode for OrderedFloat<T> | ||
where | ||
T: Encode + Send + Sync, | ||
{ | ||
type Error = T::Error; | ||
|
||
async fn encode<W>(&self, writer: &mut W) -> Result<(), Self::Error> | ||
where | ||
W: Write, | ||
{ | ||
self.0.encode(writer).await | ||
} | ||
|
||
fn size(&self) -> usize { | ||
Encode::size(&self.0) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use std::io::Cursor; | ||
|
||
use ordered_float::OrderedFloat; | ||
use tokio::io::AsyncSeekExt; | ||
|
||
use crate::serdes::{Decode, Encode}; | ||
|
||
#[tokio::test] | ||
async fn test_encode_decode() { | ||
let source_0 = OrderedFloat(32f32); | ||
let source_1 = OrderedFloat(64f64); | ||
|
||
let mut bytes = Vec::new(); | ||
let mut cursor = Cursor::new(&mut bytes); | ||
|
||
source_0.encode(&mut cursor).await.unwrap(); | ||
source_1.encode(&mut cursor).await.unwrap(); | ||
|
||
cursor.seek(std::io::SeekFrom::Start(0)).await.unwrap(); | ||
let decoded_0 = OrderedFloat::<f32>::decode(&mut cursor).await.unwrap(); | ||
let decoded_1 = OrderedFloat::<f64>::decode(&mut cursor).await.unwrap(); | ||
|
||
assert_eq!(source_0, decoded_0); | ||
assert_eq!(source_1, decoded_1); | ||
} | ||
} |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I think it is better to re-export this type