6
6
7
7
use core:: ffi:: c_void;
8
8
use core:: fmt;
9
- #[ cfg( CONFIG_RUST_ALLOC ) ]
10
- use core:: mem;
11
9
12
10
use zephyr_sys:: { k_queue, k_queue_append, k_queue_get, k_queue_init} ;
13
11
14
- #[ cfg( CONFIG_RUST_ALLOC ) ]
15
- use crate :: error:: Result ;
16
- use crate :: object:: { Fixed , StaticKernelObject , Wrapped } ;
12
+ use crate :: object:: { ObjectInit , ZephyrObject } ;
17
13
use crate :: time:: Timeout ;
18
14
19
15
/// A wrapper around a Zephyr `k_queue` object.
20
- pub struct Queue {
21
- pub ( crate ) item : Fixed < k_queue > ,
22
- }
23
-
24
- unsafe impl Sync for StaticKernelObject < k_queue > { }
16
+ pub struct Queue ( pub ( crate ) ZephyrObject < k_queue > ) ;
25
17
26
18
unsafe impl Sync for Queue { }
27
19
unsafe impl Send for Queue { }
@@ -33,13 +25,8 @@ impl Queue {
33
25
///
34
26
/// **Note**: When a Queue is dropped, any messages that have been added to the queue will be
35
27
/// leaked.
36
- #[ cfg( CONFIG_RUST_ALLOC ) ]
37
- pub fn new ( ) -> Result < Queue > {
38
- let item: Fixed < k_queue > = Fixed :: new ( unsafe { mem:: zeroed ( ) } ) ;
39
- unsafe {
40
- k_queue_init ( item. get ( ) ) ;
41
- }
42
- Ok ( Queue { item } )
28
+ pub const fn new ( ) -> Queue {
29
+ Queue ( <ZephyrObject < k_queue > >:: new_raw ( ) )
43
30
}
44
31
45
32
/// Append an element to the end of a queue.
@@ -61,7 +48,7 @@ impl Queue {
61
48
/// These are easiest to satisfy by ensuring the message is Boxed, and owned by the queue
62
49
/// system.
63
50
pub unsafe fn send ( & self , data : * mut c_void ) {
64
- k_queue_append ( self . item . get ( ) , data)
51
+ k_queue_append ( self . 0 . get ( ) , data)
65
52
}
66
53
67
54
/// Get an element from a queue.
@@ -84,42 +71,22 @@ impl Queue {
84
71
T : Into < Timeout > ,
85
72
{
86
73
let timeout: Timeout = timeout. into ( ) ;
87
- k_queue_get ( self . item . get ( ) , timeout. 0 )
74
+ k_queue_get ( self . 0 . get ( ) , timeout. 0 )
88
75
}
89
76
}
90
77
91
- impl Wrapped for StaticKernelObject < k_queue > {
92
- type T = Queue ;
93
-
94
- type I = ( ) ;
95
-
96
- fn get_wrapped ( & self , _arg : Self :: I ) -> Queue {
97
- let ptr = self . value . get ( ) ;
78
+ impl ObjectInit < k_queue > for ZephyrObject < k_queue > {
79
+ fn init ( item : * mut k_queue ) {
80
+ // SAFETY: ZephyrObject handles initialization and move prevention.
98
81
unsafe {
99
- k_queue_init ( ptr) ;
100
- }
101
- Queue {
102
- item : Fixed :: Static ( ptr) ,
82
+ k_queue_init ( item) ;
103
83
}
104
84
}
105
85
}
106
86
107
- /// A statically defined Zephyr `k_queue`.
108
- ///
109
- /// This should be declared as follows:
110
- /// ```
111
- /// kobj_define! {
112
- /// static MY_QUEUE: StaticQueue;
113
- /// }
114
- ///
115
- /// let my_queue = MY_QUEUE.init_once(());
116
- ///
117
- /// my_queue.send(...);
118
- /// ```
119
- pub type StaticQueue = StaticKernelObject < k_queue > ;
120
-
121
87
impl fmt:: Debug for Queue {
122
88
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
123
- write ! ( f, "sys::Queue {:?}" , self . item. get( ) )
89
+ // SAFETY: Just getting the address to print.
90
+ write ! ( f, "sys::Queue {:?}" , unsafe { self . 0 . get( ) } )
124
91
}
125
92
}
0 commit comments