Skip to content

Commit

Permalink
Improve API ergonomics with input type polymorphism (#34)
Browse files Browse the repository at this point in the history
- allow specifying `OperationKind` (without converting it to`packet::Operation` manually) in `Client::packet()` method
  • Loading branch information
50U10FCA7 authored Nov 27, 2024
1 parent 89515f7 commit dc064d4
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 22 deletions.
6 changes: 3 additions & 3 deletions core/examples/c_port_low_level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ fn main() {
data_size: 0,
});
user_data.set_data(accounts);
let mut packet = client.packet(user_data, tb::OperationKind::CreateAccounts.into());
let mut packet = client.packet(user_data, tb::OperationKind::CreateAccounts);
println!("Creating accounts...");
let mut state = CTX.state.lock().unwrap();
(user_data, state) = CTX.send_request(state, packet).unwrap();
Expand Down Expand Up @@ -83,7 +83,7 @@ fn main() {
.with_amount(1)
});
user_data.set_data(transfers);
packet = client.packet(user_data, tb::OperationKind::CreateTransfers.into());
packet = client.packet(user_data, tb::OperationKind::CreateTransfers);

let now = Instant::now();
(user_data, state) = CTX.send_request(state, packet).unwrap();
Expand Down Expand Up @@ -121,7 +121,7 @@ fn main() {
println!("Looking up accounts ...");
let ids = accounts.map(|a| a.id());
user_data.set_data(ids);
packet = client.packet(user_data, tb::OperationKind::LookupAccounts.into());
packet = client.packet(user_data, tb::OperationKind::LookupAccounts);
(_, state) = CTX.send_request(state, packet).unwrap();
let accounts = state.get_data::<tb::Account>();
if accounts.is_empty() {
Expand Down
2 changes: 1 addition & 1 deletion core/src/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl<'a, U> ClientHandle<'a, U>
where
U: UserDataPtr,
{
pub fn packet(self, user_data: U, operation: packet::Operation) -> Packet<'a, U> {
pub fn packet(self, user_data: U, operation: impl Into<packet::Operation>) -> Packet<'a, U> {
Packet::new(self, user_data, operation)
}
}
2 changes: 1 addition & 1 deletion core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ where
pub fn packet(
&self,
user_data: F::UserDataPtr,
operation: packet::Operation,
operation: impl Into<packet::Operation>,
) -> Packet<'_, F::UserDataPtr> {
self.handle().packet(user_data, operation)
}
Expand Down
28 changes: 11 additions & 17 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl Client {
Ok(self
.submit(
accounts.into_as_bytes(),
core::OperationKind::CreateAccounts.into(),
core::OperationKind::CreateAccounts,
)
.await?
.into_create_accounts()?)
Expand All @@ -62,7 +62,7 @@ impl Client {
Ok(self
.submit(
transfers.into_as_bytes(),
core::OperationKind::CreateTransfers.into(),
core::OperationKind::CreateTransfers,
)
.await?
.into_create_transfers()?)
Expand All @@ -78,7 +78,7 @@ impl Client {
let filter: SendOwnedSlice<account::Filter> = SendOwnedSlice::from_single(filter);
self.submit(
filter.into_as_bytes(),
core::OperationKind::GetAccountBalances.into(),
core::OperationKind::GetAccountBalances,
)
.await
.map(Reply::into_get_account_balances)
Expand All @@ -91,7 +91,7 @@ impl Client {
let filter: SendOwnedSlice<account::Filter> = SendOwnedSlice::from_single(filter);
self.submit(
filter.into_as_bytes(),
core::OperationKind::GetAccountTransfers.into(),
core::OperationKind::GetAccountTransfers,
)
.await
.map(Reply::into_get_account_transfers)
Expand All @@ -105,12 +105,9 @@ impl Client {
if ids.is_empty() {
return Ok(Vec::new());
}
self.submit(
ids.into_as_bytes(),
core::OperationKind::LookupAccounts.into(),
)
.await
.map(Reply::into_lookup_accounts)
self.submit(ids.into_as_bytes(), core::OperationKind::LookupAccounts)
.await
.map(Reply::into_lookup_accounts)
}

pub async fn lookup_transfers<T>(&self, ids: T) -> Result<Vec<Transfer>, SendError>
Expand All @@ -121,18 +118,15 @@ impl Client {
if ids.is_empty() {
return Ok(Vec::new());
}
self.submit(
ids.into_as_bytes(),
core::OperationKind::LookupTransfers.into(),
)
.await
.map(Reply::into_lookup_transfers)
self.submit(ids.into_as_bytes(), core::OperationKind::LookupTransfers)
.await
.map(Reply::into_lookup_transfers)
}

async fn submit(
&self,
data: SendAsBytesOwnedSlice,
operation: core::Operation,
operation: impl Into<core::Operation>,
) -> Result<Reply, SendError> {
let (reply_sender, reply_receiver) = oneshot::channel();
let user_data = Box::new(UserData { reply_sender, data });
Expand Down

0 comments on commit dc064d4

Please sign in to comment.