hidra 0.0.1

Pure Rust HID library.
Documentation
//! Minimal executor for exercising the crate's futures in unit tests
//! without an async runtime dependency.
//!
//! Only some configurations use it (the Linux `reactor` tests and the `nusb`
//! backend tests), so it is dead code on, for example, a default-feature test
//! build on macOS or Windows.
#![allow(dead_code)]

use std::future::Future;
use std::sync::Arc;
use std::task::{Context, Poll, Wake, Waker};
use std::thread::{self, Thread};

struct ThreadWaker(Thread);

impl Wake for ThreadWaker {
    fn wake(self: Arc<Self>) {
        self.0.unpark();
    }
}

/// Drive a future to completion on the current thread.
pub(crate) fn block_on<F: Future>(fut: F) -> F::Output {
    let waker = Waker::from(Arc::new(ThreadWaker(thread::current())));
    let mut cx = Context::from_waker(&waker);
    let mut fut = std::pin::pin!(fut);
    loop {
        match fut.as_mut().poll(&mut cx) {
            Poll::Ready(value) => return value,
            Poll::Pending => thread::park(),
        }
    }
}