Skip to content

Commit

Permalink
Add support for f32 and f64 types #4
Browse files Browse the repository at this point in the history
  • Loading branch information
fafhrd91 committed Jan 17, 2024
1 parent f8004b3 commit ca8e6a2
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 75 deletions.
4 changes: 4 additions & 0 deletions ntex-grpc/CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changes

## [0.6.1] - 2024-01-17

* Add support for f32 and f64 types #4

## [0.6.0] - 2024-01-09

* Release
Expand Down
2 changes: 1 addition & 1 deletion ntex-grpc/src/client/connector.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{cell::RefCell, future::Future, rc::Rc};
use std::{cell::RefCell, rc::Rc};

use ntex_connect::{Address, Connect, ConnectError, Connector as DefaultConnector};
use ntex_h2::{self as h2, client};
Expand Down
6 changes: 3 additions & 3 deletions ntex-grpc/src/client/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::future::Future;
#![allow(async_fn_in_trait)]

use ntex_bytes::Bytes;
use ntex_h2::{client, OperationError, StreamError};
Expand All @@ -15,11 +15,11 @@ pub trait Transport<T: MethodDef> {
/// Errors produced by the transport.
type Error: From<HttpError>;

fn request(
async fn request(
&self,
args: &T::Input,
ctx: RequestContext,
) -> impl Future<Output = Result<Response<T>, Self::Error>>;
) -> Result<Response<T>, Self::Error>;
}

/// Client utils methods
Expand Down
51 changes: 0 additions & 51 deletions ntex-grpc/src/error.rs

This file was deleted.

5 changes: 1 addition & 4 deletions ntex-grpc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
mod consts;
//mod error;
mod service;
mod status;
mod utils;
Expand All @@ -9,8 +8,6 @@ pub mod server;
pub mod types;

pub use crate::encoding::DecodeError;
// pub use crate::error::ServiceError;
//pub use ntex_http::Error as HttpError;

pub use crate::service::{MethodDef, ServiceDef};
pub use crate::status::GrpcStatus;
Expand All @@ -28,7 +25,7 @@ pub use ntex_bytes::{ByteString, Bytes, BytesMut};
#[doc(hidden)]
pub use ntex_service::{Service, ServiceCtx, ServiceFactory};
#[doc(hidden)]
pub use ntex_util::{future::BoxFuture, HashMap};
pub use ntex_util::HashMap;

// [1]: https://github.com/serde-rs/serde/blob/v1.0.89/serde/src/lib.rs#L245-L256
#[allow(unused_imports)]
Expand Down
78 changes: 62 additions & 16 deletions ntex-grpc/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{collections::HashMap, convert::TryFrom, fmt, hash::BuildHasher, hash::Hash, mem};

use ntex_bytes::{ByteString, Bytes, BytesMut};
use ntex_bytes::{Buf, BufMut, ByteString, Bytes, BytesMut};

pub use crate::encoding::WireType;
use crate::encoding::{self, DecodeError};
Expand Down Expand Up @@ -101,6 +101,7 @@ pub trait NativeType: PartialEq + Default + Sized + fmt::Debug {
src: &mut Bytes,
) -> Result<(), DecodeError> {
encoding::check_wire_type(Self::TYPE, wtype)?;

if Self::TYPE == WireType::Varint {
self.merge(src)
} else {
Expand Down Expand Up @@ -559,21 +560,66 @@ varint!(i64, 0i64);
varint!(u32, 0u32);
varint!(u64, 0u64);

// varint!(i32, sint32,
// to_uint64(value) {
// ((value << 1) ^ (value >> 31)) as u32 as u64
// },
// from_uint64(value) {
// let value = value as u32;
// ((value >> 1) as i32) ^ (-((value & 1) as i32))
// });
// varint!(i64, sint64,
// to_uint64(value) {
// ((value << 1) ^ (value >> 63)) as u64
// },
// from_uint64(value) {
// ((value >> 1) as i64) ^ (-((value & 1) as i64))
// });
/// Macro which emits a module containing a set of encoding functions for a
/// fixed width numeric type.
macro_rules! fixed_width {
($ty:ty,
$width:expr,
$wire_type:expr,
$default:expr,
$put:expr,
$get:expr) => {
impl NativeType for $ty {
const TYPE: WireType = $wire_type;

#[inline]
fn is_default(&self) -> bool {
*self == $default
}

#[inline]
fn encode_value(&self, dst: &mut BytesMut) {
$put(dst, *self);
}

#[inline]
fn encoded_len(&self, tag: u32) -> usize {
encoding::key_len(tag) + $width
}

#[inline]
fn value_len(&self) -> usize {
$width
}

#[inline]
fn merge(&mut self, src: &mut Bytes) -> Result<(), DecodeError> {
if src.len() < $width {
return Err(DecodeError::new("Buffer underflow"));
}
*self = $get(src);
Ok(())
}
}
};
}

fixed_width!(
f32,
4,
WireType::ThirtyTwoBit,
0f32,
BufMut::put_f32_le,
Buf::get_f32_le
);
fixed_width!(
f64,
8,
WireType::SixtyFourBit,
0f64,
BufMut::put_f64_le,
Buf::get_f64_le
);

#[cfg(test)]
mod tests {
Expand Down

0 comments on commit ca8e6a2

Please sign in to comment.