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

Added EventHandler and EventHandlerMut traits #3

Merged
merged 1 commit into from
Jun 5, 2023

Conversation

louisponet
Copy link
Contributor

This changes the API to take in types that implement the EventHandler and EventHandlerMut traits rather than standard Fn.

This allows for functionality like:

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 Report

Merging #3 (cd0ddbe) into master (5b280ab) will increase coverage by 0.0%.
The diff coverage is 73.0%.

❗ 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 changed the base branch from master to feature/eventhandler June 5, 2023 21:19
@sklose
Copy link
Owner

sklose commented Jun 5, 2023

thanks for this PR @louisponet. I am going to merge this into a feature branch first so I can resolve the CI build issues. I will publish a new release once I am done.

@sklose sklose merged commit beb2614 into sklose:feature/eventhandler Jun 5, 2023
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