Skip to main content

zerodds_corba_rt/
current.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2026 ZeroDDS Contributors
3
4//! `RTCORBA::Current` (RT-CORBA §5.5) — read/write access to the CORBA
5//! priority of the current execution context.
6
7use crate::priority::Priority;
8
9/// `RTCORBA::Current` (§5.5): holds the CORBA priority of the current context.
10/// Setting it changes (in a real ORB) the native thread priority via the
11/// active [`PriorityMapping`](crate::priority::PriorityMapping).
12#[derive(Debug, Clone, Copy)]
13pub struct RtCurrent {
14    priority: Priority,
15}
16
17impl RtCurrent {
18    /// New `Current` with an initial priority.
19    #[must_use]
20    pub fn new(priority: Priority) -> Self {
21        Self { priority }
22    }
23
24    /// `get_priority` (§5.5) — the current CORBA priority.
25    #[must_use]
26    pub fn get_priority(&self) -> Priority {
27        self.priority
28    }
29
30    /// `set_priority` (§5.5) — sets the CORBA priority of the context.
31    pub fn set_priority(&mut self, priority: Priority) {
32        self.priority = priority;
33    }
34}
35
36impl Default for RtCurrent {
37    fn default() -> Self {
38        // Default priority 0 is always valid.
39        Self {
40            priority: Priority::new(0).unwrap_or_else(|| Priority::clamped(0)),
41        }
42    }
43}
44
45#[cfg(test)]
46#[allow(clippy::unwrap_used, clippy::panic)]
47mod tests {
48    use super::*;
49
50    #[test]
51    fn get_set_priority() {
52        let mut cur = RtCurrent::new(Priority::new(10).unwrap());
53        assert_eq!(cur.get_priority().value(), 10);
54        cur.set_priority(Priority::new(50).unwrap());
55        assert_eq!(cur.get_priority().value(), 50);
56    }
57
58    #[test]
59    fn default_is_zero() {
60        assert_eq!(RtCurrent::default().get_priority().value(), 0);
61    }
62}