Skip to content

Commit

Permalink
refactor: use impl future instead of async-trait in most traits
Browse files Browse the repository at this point in the history
  • Loading branch information
negezor committed Mar 31, 2024
1 parent db802c1 commit d609de5
Show file tree
Hide file tree
Showing 18 changed files with 269 additions and 251 deletions.
23 changes: 12 additions & 11 deletions klickhouse/src/io.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
use crate::{KlickhouseError, Result};
use futures::Future;
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};

use crate::{KlickhouseError, Result};

use crate::protocol::MAX_STRING_SIZE;

#[async_trait::async_trait]
pub trait ClickhouseRead: AsyncRead + Unpin + Send + Sync {
async fn read_var_uint(&mut self) -> Result<u64>;
fn read_var_uint(&mut self) -> impl Future<Output = Result<u64>> + Send;

async fn read_string(&mut self) -> Result<Vec<u8>>;
fn read_string(&mut self) -> impl Future<Output = Result<Vec<u8>>> + Send;

async fn read_utf8_string(&mut self) -> Result<String> {
Ok(String::from_utf8(self.read_string().await?)?)
fn read_utf8_string(&mut self) -> impl Future<Output = Result<String>> + Send {
async { Ok(String::from_utf8(self.read_string().await?)?) }
}
}

#[async_trait::async_trait]
impl<T: AsyncRead + Unpin + Send + Sync> ClickhouseRead for T {
async fn read_var_uint(&mut self) -> Result<u64> {
let mut out = 0u64;
Expand Down Expand Up @@ -50,14 +50,15 @@ impl<T: AsyncRead + Unpin + Send + Sync> ClickhouseRead for T {
}
}

#[async_trait::async_trait]
pub trait ClickhouseWrite: AsyncWrite + Unpin + Send + Sync + 'static {
async fn write_var_uint(&mut self, value: u64) -> Result<()>;
fn write_var_uint(&mut self, value: u64) -> impl Future<Output = Result<()>> + Send;

async fn write_string(&mut self, value: impl AsRef<[u8]> + Send) -> Result<()>;
fn write_string(
&mut self,
value: impl AsRef<[u8]> + Send,
) -> impl Future<Output = Result<()>> + Send;
}

#[async_trait::async_trait]
impl<T: AsyncWrite + Unpin + Send + Sync + 'static> ClickhouseWrite for T {
async fn write_var_uint(&mut self, mut value: u64) -> Result<()> {
for _ in 0..9u64 {
Expand Down
1 change: 0 additions & 1 deletion klickhouse/src/types/deserialize/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ impl ArrayDeserializerGeneric for ArrayDeserializer {
}
}

#[async_trait::async_trait]
impl<T: ArrayDeserializerGeneric + 'static> Deserializer for T {
async fn read_prefix<R: ClickhouseRead>(
type_: &Type,
Expand Down
1 change: 0 additions & 1 deletion klickhouse/src/types/deserialize/geo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use crate::values;

pub struct PointDeserializer;

#[async_trait::async_trait]
impl Deserializer for PointDeserializer {
async fn read_prefix<R: ClickhouseRead>(
_type_: &Type,
Expand Down
1 change: 0 additions & 1 deletion klickhouse/src/types/deserialize/low_cardinality.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use crate::types::low_cardinality::*;

pub struct LowCardinalityDeserializer;

#[async_trait::async_trait]
impl Deserializer for LowCardinalityDeserializer {
async fn read_prefix<R: ClickhouseRead>(
_type_: &Type,
Expand Down
1 change: 0 additions & 1 deletion klickhouse/src/types/deserialize/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use super::{Deserializer, DeserializerState, Type};

pub struct MapDeserializer;

#[async_trait::async_trait]
impl Deserializer for MapDeserializer {
async fn read_prefix<R: ClickhouseRead>(
type_: &Type,
Expand Down
1 change: 0 additions & 1 deletion klickhouse/src/types/deserialize/nullable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use super::{Deserializer, DeserializerState, Type};

pub struct NullableDeserializer;

#[async_trait::async_trait]
impl Deserializer for NullableDeserializer {
async fn read_prefix<R: ClickhouseRead>(
type_: &Type,
Expand Down
1 change: 0 additions & 1 deletion klickhouse/src/types/deserialize/sized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use super::{Deserializer, DeserializerState, Type};

pub struct SizedDeserializer;

#[async_trait::async_trait]
impl Deserializer for SizedDeserializer {
async fn read<R: ClickhouseRead>(
type_: &Type,
Expand Down
1 change: 0 additions & 1 deletion klickhouse/src/types/deserialize/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use super::{Deserializer, DeserializerState, Type};
pub struct StringDeserializer;

#[allow(clippy::uninit_vec)]
#[async_trait::async_trait]
impl Deserializer for StringDeserializer {
async fn read<R: ClickhouseRead>(
type_: &Type,
Expand Down
1 change: 0 additions & 1 deletion klickhouse/src/types/deserialize/tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use super::{Deserializer, DeserializerState, Type};

pub struct TupleDeserializer;

#[async_trait::async_trait]
impl Deserializer for TupleDeserializer {
async fn read_prefix<R: ClickhouseRead>(
type_: &Type,
Expand Down
Loading

0 comments on commit d609de5

Please sign in to comment.