Skip to content
เข้าสู่ระบบ

Async Enums

Enums สำหรับ async programming

use std::task::Poll;
use std::future::Future;
use std::pin::Pin;
use std::task::Context;
// Poll is the core of Future
enum ExamplePoll<T> {
Ready(T), // Value is ready
Pending, // Not ready yet, will wake
}
// Custom future
struct CountDown {
remaining: u32,
}
impl Future for CountDown {
type Output = &'static str;
fn poll(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Self::Output> {
if self.remaining == 0 {
Poll::Ready("Done!")
} else {
self.remaining -= 1;
// In real code: schedule wake-up
// cx.waker().wake_by_ref();
Poll::Pending
}
}
}
fn main() {
// Poll variants
let ready: Poll<i32> = Poll::Ready(42);
let pending: Poll<i32> = Poll::Pending;
// Check status
println!("ready.is_ready(): {}", ready.is_ready());
println!("pending.is_pending(): {}", pending.is_pending());
// Map the value
let mapped = ready.map(|x| x * 2);
println!("Mapped: {:?}", mapped);
// Poll methods
if let Poll::Ready(value) = ready {
println!("Value: {}", value);
}
}

เข้าสู่ระบบเพื่อดูเนื้อหาเต็ม

ยืนยันตัวตนด้วยบัญชี Google เพื่อปลดล็อกบทความทั้งหมด