Skip to content

Commit

Permalink
allocation free on Handlers that use closures #27 and #26
Browse files Browse the repository at this point in the history
  • Loading branch information
mimran1980 committed Jan 18, 2025
1 parent e93a24f commit f6a9d9d
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions rusteron-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,18 @@ In this example, the `AeronErrorHandlerCallback` trait is implemented by `AeronE

### 2. Using a Closure

Alternatively, you can use closures as handlers. However, due to lifetime issues, all arguments are owned, which results in allocations (e.g., converting strings). This method is not suitable for performance-sensitive roles but is more convenient for simpler, non-critical scenarios. Example:
Alternatively, you can use closures as handlers. However, all arguments must be copied if your planning to use them later, even ones with static lifetimes. This method is not suitable for performance-sensitive roles but is more convenient for simpler, non-critical scenarios. Example:

```rust ,no_run
use rusteron_client::*;

pub struct AeronErrorHandlerClosure<F: FnMut(::std::os::raw::c_int, String) -> ()> {
pub struct AeronErrorHandlerClosure<F: FnMut(::std::os::raw::c_int, &'static str) -> ()> {
closure: F,
}

impl<F: FnMut(::std::os::raw::c_int, String) -> ()> AeronErrorHandlerCallback for AeronErrorHandlerClosure<F> {
fn handle_aeron_error_handler(&mut self, errcode: ::std::os::raw::c_int, message: &str) -> () {
(self.closure)(errcode.to_owned(), message.to_owned())
impl<F: FnMut(::std::os::raw::c_int, &'static str) -> ()> AeronErrorHandlerCallback for AeronErrorHandlerClosure<F> {
fn handle_aeron_error_handler(&mut self, errcode: ::std::os::raw::c_int, message: &'static str) -> () {
(self.closure)(errcode, message)
}
}
```
Expand Down

0 comments on commit f6a9d9d

Please sign in to comment.