Skip to main content

EventLoop

Struct EventLoop 

Source
pub struct EventLoop { /* private fields */ }

Implementations§

Source§

impl EventLoop

Source

pub fn run(self) -> Result<(), Error>

Examples found in repository?
examples/server.rs (line 73)
35fn main() {
36    nwep::init().unwrap_or_else(|e| {
37        eprintln!("init: {e}");
38        process::exit(1);
39    });
40
41    let key_file = std::env::var("NWEP_KEY_FILE").unwrap_or_else(|_| DEFAULT_KEY_FILE.into());
42    let keypair = load_or_generate_keypair(&key_file).unwrap_or_else(|e| {
43        eprintln!("keypair: {e}");
44        process::exit(1);
45    });
46
47    let node_id = keypair.node_id().unwrap_or_else(|e| {
48        eprintln!("node_id: {e}");
49        process::exit(1);
50    });
51    eprintln!("node id: {node_id}");
52
53    let mut router = Router::new();
54    router.handle_func("/hello", |w: &mut ResponseWriter, _r: &Request| {
55        let _ = w.respond("ok", b"hello from nwep-rust");
56    });
57
58    let addr = std::env::args().nth(1).unwrap_or_else(|| DEFAULT_ADDR.into());
59
60    let (server, event_loop) = ServerBuilder::new(&addr, keypair)
61        .on_connect(|info| eprintln!("connected: {}", info.node_id))
62        .on_disconnect(|info, code| eprintln!("disconnected: {} (code {code})", info.node_id))
63        .build(router)
64        .unwrap_or_else(|e| {
65            eprintln!("server: {e}");
66            process::exit(1);
67        });
68
69    let url = server.url("/hello");
70    eprintln!("listening on {}", server.addr());
71    println!("{url}"); // URL printed to stdout for the client to read
72
73    if let Err(e) = event_loop.run() {
74        eprintln!("server run: {e}");
75        process::exit(1);
76    }
77}

Trait Implementations§

Source§

impl Drop for EventLoop

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl Send for EventLoop

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.