Merge pull request #1049 from k-kinzal/support-primitive-type-for-cursor-type

Support for primitive type in CursorType
This commit is contained in:
Sunli 2022-09-06 09:41:59 +08:00 committed by GitHub
commit c0e4c2a514
1 changed files with 38 additions and 30 deletions

View File

@ -1,7 +1,9 @@
use std::{
char::ParseCharError,
convert::Infallible,
fmt::Display,
num::{ParseFloatError, ParseIntError},
str::ParseBoolError,
};
use serde::{de::DeserializeOwned, Serialize};
@ -23,8 +25,26 @@ pub trait CursorType: Sized {
fn encode_cursor(&self) -> String;
}
impl CursorType for usize {
type Error = ParseIntError;
macro_rules! cursor_type_int_impl {
($($t:ty)*) => {$(
impl CursorType for $t {
type Error = ParseIntError;
fn decode_cursor(s: &str) -> Result<Self, Self::Error> {
s.parse()
}
fn encode_cursor(&self) -> String {
self.to_string()
}
}
)*}
}
cursor_type_int_impl! { isize i8 i16 i32 i64 i128 usize u8 u16 u32 u64 u128 }
impl CursorType for f32 {
type Error = ParseFloatError;
fn decode_cursor(s: &str) -> Result<Self, Self::Error> {
s.parse()
@ -35,8 +55,8 @@ impl CursorType for usize {
}
}
impl CursorType for i32 {
type Error = ParseIntError;
impl CursorType for f64 {
type Error = ParseFloatError;
fn decode_cursor(s: &str) -> Result<Self, Self::Error> {
s.parse()
@ -47,8 +67,20 @@ impl CursorType for i32 {
}
}
impl CursorType for i64 {
type Error = ParseIntError;
impl CursorType for char {
type Error = ParseCharError;
fn decode_cursor(s: &str) -> Result<Self, Self::Error> {
s.parse()
}
fn encode_cursor(&self) -> String {
self.to_string()
}
}
impl CursorType for bool {
type Error = ParseBoolError;
fn decode_cursor(s: &str) -> Result<Self, Self::Error> {
s.parse()
@ -83,30 +115,6 @@ impl CursorType for ID {
}
}
impl CursorType for f64 {
type Error = ParseFloatError;
fn decode_cursor(s: &str) -> Result<Self, Self::Error> {
s.parse()
}
fn encode_cursor(&self) -> String {
self.to_string()
}
}
impl CursorType for f32 {
type Error = ParseFloatError;
fn decode_cursor(s: &str) -> Result<Self, Self::Error> {
s.parse()
}
fn encode_cursor(&self) -> String {
self.to_string()
}
}
/// A opaque cursor that encode/decode the value to base64
pub struct OpaqueCursor<T>(T);