Skip to content

Commit

Permalink
rust/fbthrift_ext/adapters: RFC: add UsizeAdapter
Browse files Browse the repository at this point in the history
Summary: Adding a thrift adapter for `usize`, similar to those for the explicitly sized unsigned integer types. Only implementing it for 64-bit platforms for now, but this can be trivially extended if/when needed

Reviewed By: Imxset21

Differential Revision: D66657692

fbshipit-source-id: 69f1aa836ddf9b74e881e5cfdfb2a8efc6793a52
  • Loading branch information
Sergey Anpilov authored and facebook-github-bot committed Dec 3, 2024
1 parent 8f36a1c commit 24d6c4d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
3 changes: 3 additions & 0 deletions shed/fbthrift_ext/adapters/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,5 +121,8 @@ pub use crate::redacted::RedactedAdapter;
pub use crate::socket_addr::SocketAddrAdapter;
#[doc(inline)]
pub use crate::unsigned_int::UnsignedIntAdapter;
#[cfg(target_pointer_width = "64")]
#[doc(inline)]
pub use crate::unsigned_int::UsizeAdapter;
#[doc(inline)]
pub use crate::uuid::UuidAdapter;
32 changes: 26 additions & 6 deletions shed/fbthrift_ext/adapters/unsigned_int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ use fbthrift::adapter::ThriftAdapter;
pub struct UnsignedIntAdapter<T>(PhantomData<T>);

macro_rules! unsigned_int_adapter {
($thrift:ty, $rust:ty) => {
impl ThriftAdapter for UnsignedIntAdapter<$thrift> {
($thrift:ty, $rust:ty, $adapter:ty) => {
impl ThriftAdapter for $adapter {
type StandardType = $thrift;
type AdaptedType = $rust;
type Error = std::convert::Infallible;
Expand All @@ -47,7 +47,27 @@ macro_rules! unsigned_int_adapter {
};
}

unsigned_int_adapter!(i8, u8);
unsigned_int_adapter!(i16, u16);
unsigned_int_adapter!(i32, u32);
unsigned_int_adapter!(i64, u64);
unsigned_int_adapter!(i8, u8, UnsignedIntAdapter<i8>);
unsigned_int_adapter!(i16, u16, UnsignedIntAdapter<i16>);
unsigned_int_adapter!(i32, u32, UnsignedIntAdapter<i32>);
unsigned_int_adapter!(i64, u64, UnsignedIntAdapter<i64>);

#[cfg(target_pointer_width = "64")]
/// Adapts the platform-appropriate signed Thrift integer type to `usize`
///
/// # Examples
///
/// ```thrift
/// include "thrift/annotation/rust.thrift";
///
/// @rust.Adapter{name = "::fbthrift_adapters::UsizeAdapter"}
/// typedef i64 Usize;
///
/// struct Parameters {
/// 1: Usize my_param;
/// }
/// ```
pub struct UsizeAdapter(PhantomData<i64>);

#[cfg(target_pointer_width = "64")]
unsigned_int_adapter!(i64, usize, UsizeAdapter);

0 comments on commit 24d6c4d

Please sign in to comment.