Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finalize EventHandler API #4

Merged
merged 4 commits into from
Jun 5, 2023
Merged

Finalize EventHandler API #4

merged 4 commits into from
Jun 5, 2023

Conversation

sklose
Copy link
Owner

@sklose sklose commented Jun 5, 2023

No description provided.

louisponet and others added 3 commits June 5, 2023 17:21
This changes the API to take in types that implement the `EventHandler`
and `EventHandlerMut` traits rather than standard `Fn`.

This allows for functionality like:

```rust
use disrustor::{
    internal::SpinLoopWaitStrategy,
    *,
};

const MAX: i64 = 200i64;

struct Journal {
    pub i: u32,
    id: u32
}

impl Journal {
    fn new(id: u32) -> Self {
        Self {
            i: 0,
            id: id
        }
    }
}

impl EventHandler<u32> for Journal {
    fn handle_event(&mut self, event: &u32, seq: Sequence, eob: bool) {
        self.i = *event;
        println!(
            "journal: {}; msg: {}",
            self.id,
            self.i
        );
    }
}

fn main() {
    // let journal2 = std::cell::RefCell::new(Vec::<u32>::new()); // implemented somewhere else
    let (executor, producer) = DisrustorBuilder::with_ring_buffer(4096)
        .with_wait_strategy::<SpinLoopWaitStrategy>()
        .with_single_producer()
        .with_barrier(|b| {
            // journal + replication can happen in parallel
            b.handle_events(Journal::new(0));
            b.handle_events(Journal::new(1));
        })
        .build();

    let handle = executor.spawn();

    for i in 1..=MAX {
        std::thread::sleep_ms(1);
        producer.write([i], |d, _, _| {
            *d = i as u32;
        });
    }
    producer.drain();
    handle.join();
}
```

See #2
@codecov-commenter
Copy link

codecov-commenter commented Jun 5, 2023

Codecov Report

Merging #4 (45cd913) into master (5b280ab) will increase coverage by 0.0%.
The diff coverage is 73.3%.

❗ Your organization is not using the GitHub App Integration. As a result you may experience degraded service beginning May 15th. Please install the Github App Integration for your organization. Read more.

Impacted Files Coverage Δ
src/prelude.rs 75.0% <ø> (ø)
src/dsl.rs 62.8% <50.0%> (ø)
src/lib.rs 90.3% <62.5%> (+0.3%) ⬆️
src/consumer.rs 100.0% <100.0%> (ø)

@sklose sklose merged commit cff4199 into master Jun 5, 2023
@sklose sklose deleted the feature/eventhandler branch June 5, 2023 21:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants