use std::cmp::Ordering;
use std::num::NonZeroU8;
use std::ops::Mul;
use std::time::Duration;
#[derive(Clone, Debug)]
#[must_use]
pub struct ActorOptions {
priority: Priority,
ready: bool,
}
impl ActorOptions {
pub const fn priority(&self) -> Priority {
self.priority
}
pub const fn with_priority(mut self, priority: Priority) -> Self {
self.priority = priority;
self
}
pub const fn is_ready(&self) -> bool {
self.ready
}
pub const fn mark_ready(mut self, ready: bool) -> Self {
self.ready = ready;
self
}
}
impl Default for ActorOptions {
fn default() -> ActorOptions {
ActorOptions {
priority: Priority::default(),
ready: true,
}
}
}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[repr(transparent)]
pub struct Priority(NonZeroU8);
impl Priority {
pub const LOW: Priority = Priority(NonZeroU8::new(15).unwrap());
pub const NORMAL: Priority = Priority(NonZeroU8::new(10).unwrap());
pub const HIGH: Priority = Priority(NonZeroU8::new(5).unwrap());
}
impl Default for Priority {
fn default() -> Priority {
Priority::NORMAL
}
}
impl Ord for Priority {
fn cmp(&self, other: &Self) -> Ordering {
other.0.cmp(&self.0)
}
}
impl PartialOrd for Priority {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
other.0.partial_cmp(&self.0)
}
fn lt(&self, other: &Self) -> bool {
other.0 < self.0
}
fn le(&self, other: &Self) -> bool {
other.0 <= self.0
}
fn gt(&self, other: &Self) -> bool {
other.0 > self.0
}
fn ge(&self, other: &Self) -> bool {
other.0 >= self.0
}
}
#[doc(hidden)]
impl Mul<Priority> for Duration {
type Output = Duration;
fn mul(self, rhs: Priority) -> Duration {
self * u32::from(rhs.0.get())
}
}
#[test]
fn priority_duration_multiplication() {
let duration = Duration::from_millis(1);
let high = duration * Priority::HIGH;
let normal = duration * Priority::NORMAL;
let low = duration * Priority::LOW;
assert!(high < normal);
assert!(normal < low);
assert!(high < low);
}
#[derive(Debug, Default)]
#[must_use]
pub struct SyncActorOptions {
thread_name: Option<String>,
}
impl SyncActorOptions {
pub fn name(&self) -> Option<&str> {
self.thread_name.as_deref()
}
pub(crate) fn take_name(self) -> Option<String> {
self.thread_name
}
pub fn with_name(mut self, thread_name: String) -> Self {
self.thread_name = Some(thread_name);
self
}
}
#[derive(Clone, Debug, Default)]
#[must_use]
pub struct FutureOptions {
priority: Priority,
}
impl FutureOptions {
pub const fn priority(&self) -> Priority {
self.priority
}
pub const fn with_priority(mut self, priority: Priority) -> Self {
self.priority = priority;
self
}
}