feat(server-framework): add Server trait, Encode/Decode, async executor, syscall wrappers, and mock arch for host testing
This commit is contained in:
35
crates/server-framework/src/executor/mod.rs
Normal file
35
crates/server-framework/src/executor/mod.rs
Normal file
@@ -0,0 +1,35 @@
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
use core::future::Future;
|
||||
use core::pin::Pin;
|
||||
use core::task::{Context, Poll, RawWaker, RawWakerVTable, Waker};
|
||||
|
||||
fn noop_raw_waker() -> RawWaker {
|
||||
fn no_op(_: *const ()) {}
|
||||
fn clone(_: *const ()) -> RawWaker { noop_raw_waker() }
|
||||
RawWaker::new(
|
||||
core::ptr::null(),
|
||||
&RawWakerVTable::new(clone, no_op, no_op, no_op),
|
||||
)
|
||||
}
|
||||
|
||||
fn noop_waker() -> Waker {
|
||||
unsafe { Waker::from_raw(noop_raw_waker()) }
|
||||
}
|
||||
|
||||
pub fn block_on<F: Future>(future: F) -> F::Output {
|
||||
let waker = noop_waker();
|
||||
let mut cx = Context::from_waker(&waker);
|
||||
let mut future = core::pin::pin!(future);
|
||||
|
||||
loop {
|
||||
match future.as_mut().poll(&mut cx) {
|
||||
Poll::Ready(output) => return output,
|
||||
Poll::Pending => {
|
||||
#[cfg(not(test))]
|
||||
userspace::syscall::yield_now();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
17
crates/server-framework/src/executor/tests.rs
Normal file
17
crates/server-framework/src/executor/tests.rs
Normal file
@@ -0,0 +1,17 @@
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn block_on_ready_future() {
|
||||
let result = block_on(async { 42 });
|
||||
assert_eq!(result, 42);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn block_on_chained_futures() {
|
||||
let result = block_on(async {
|
||||
let a = async { 10 }.await;
|
||||
let b = async { 20 }.await;
|
||||
a + b
|
||||
});
|
||||
assert_eq!(result, 30);
|
||||
}
|
||||
Reference in New Issue
Block a user