Skip to main content

Events

Trait Events 

Source
pub trait Events:
    Send
    + Sync
    + 'static {
    // Provided method
    fn event(&self, event: &Event<'_>) { ... }
}
Expand description

Where web-net reports. Every method has an empty default; the default implementation, NoEvents, reports nothing.

Called on the request path: keep it to a counter or a queue push.

use std::sync::atomic::{AtomicU32, Ordering};
use web_net::{Event, Events};

// Counts every event; the app reports refusals, denials, and failures here.
#[derive(Default)]
struct Counter(AtomicU32);
impl Events for Counter {
    fn event(&self, _: &Event<'_>) {
        self.0.fetch_add(1, Ordering::Relaxed);
    }
}

let app = web_net::App::builder().events(Counter::default()).build()?;

Provided Methods§

Source

fn event(&self, event: &Event<'_>)

One event.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl<T: Events + ?Sized> Events for Arc<T>

A sink the application keeps a handle to, to read what it counted.

use std::sync::Arc;
use std::sync::atomic::{AtomicU32, Ordering};
use web_net::{App, Event, Events, get};

#[derive(Default)]
struct Refusals(AtomicU32);
impl Events for Refusals {
    fn event(&self, _: &Event<'_>) {
        self.0.fetch_add(1, Ordering::Relaxed);
    }
}

let seen = Arc::new(Refusals::default());
let app = App::builder()
    .events(Arc::clone(&seen))
    .route(get("/", |cx| cx.text(200, "hi")))
    .build()?;
serve_net::testing::respond(&app, b"GET /missing HTTP/1.1\r\nHost: localhost\r\n\r\n");
assert_eq!(seen.0.load(Ordering::Relaxed), 1);
Source§

fn event(&self, event: &Event<'_>)

Implementors§