@@ -12,12 +12,8 @@ use crate::utils::assert_main_thread;
12
12
/// A transparent wrapper around a raw [`pw_loop`](`pw_sys::pw_loop`).
13
13
/// It is usually only seen in a reference (`&LoopRef`).
14
14
///
15
- /// This type is similar to rusts [`str`] type,
16
- /// where [`&str`](`std::str`) is a reference to a slice of characters,
17
- /// and [`&LoopRef`](`LoopRef`) is a reference to a [`pw_loop`](`pw_sys::pw_loop`).
18
- ///
19
- /// Like with [`str`] and [`String`], an owned version, [`Loop`], is available,
20
- /// which can create of [`pw_loop`](`pw_sys::pw_loop`) and lets you own them,
15
+ /// An owned version, [`Loop`], is available,
16
+ /// which lets you create and own a [`pw_loop`](`pw_sys::pw_loop`),
21
17
/// but other objects, such as [`MainLoop`](`crate::MainLoop`), also contain them.
22
18
#[ repr( transparent) ]
23
19
pub struct LoopRef ( pw_sys:: pw_loop ) ;
@@ -49,32 +45,34 @@ impl LoopRef {
49
45
/// Start an iteration of the loop. This function should be called
50
46
/// before calling iterate and is typically used to capture the thread
51
47
/// that this loop will run in.
52
- pub fn enter ( & self ) {
53
- unsafe {
54
- let mut iface = self . as_raw ( ) . control . as_ref ( ) . unwrap ( ) . iface ;
48
+ ///
49
+ /// # Safety
50
+ /// Each call of `enter` must be paired with a call of `leave`.
51
+ pub unsafe fn enter ( & self ) {
52
+ let mut iface = self . as_raw ( ) . control . as_ref ( ) . unwrap ( ) . iface ;
55
53
56
- spa_interface_call_method ! (
57
- & mut iface as * mut spa_sys:: spa_interface,
58
- spa_sys:: spa_loop_control_methods,
59
- enter,
60
- )
61
- }
54
+ spa_interface_call_method ! (
55
+ & mut iface as * mut spa_sys:: spa_interface,
56
+ spa_sys:: spa_loop_control_methods,
57
+ enter,
58
+ )
62
59
}
63
60
64
61
/// Leave a loop
65
62
///
66
63
/// Ends the iteration of a loop. This should be called after calling
67
64
/// iterate.
68
- pub fn leave ( & self ) {
69
- unsafe {
70
- let mut iface = self . as_raw ( ) . control . as_ref ( ) . unwrap ( ) . iface ;
65
+ ///
66
+ /// # Safety
67
+ /// Each call of `leave` must be paired with a call of `enter`.
68
+ pub unsafe fn leave ( & self ) {
69
+ let mut iface = self . as_raw ( ) . control . as_ref ( ) . unwrap ( ) . iface ;
71
70
72
- spa_interface_call_method ! (
73
- & mut iface as * mut spa_sys:: spa_interface,
74
- spa_sys:: spa_loop_control_methods,
75
- leave,
76
- )
77
- }
71
+ spa_interface_call_method ! (
72
+ & mut iface as * mut spa_sys:: spa_interface,
73
+ spa_sys:: spa_loop_control_methods,
74
+ leave,
75
+ )
78
76
}
79
77
80
78
/// Perform one iteration of the loop.
@@ -86,29 +84,41 @@ impl LoopRef {
86
84
/// up to the provided timeout and then dispatch the fds with activity.
87
85
/// The number of dispatched fds is returned.
88
86
///
89
- /// Before calling this, you should call [`Self::enter()`] on the loop, and [`Self::leave()`] afterwards.
87
+ /// This will automatically call [`Self::enter()`] on the loop before iterating , and [`Self::leave()`] afterwards.
90
88
///
91
89
/// # Panics
92
90
/// This function will panic if the provided timeout as milliseconds does not fit inside a
93
91
/// `c_int` integer.
94
92
pub fn iterate ( & self , timeout : std:: time:: Duration ) -> i32 {
95
93
unsafe {
96
- let mut iface = self . as_raw ( ) . control . as_ref ( ) . unwrap ( ) . iface ;
97
-
98
- let timeout: c_int = timeout
99
- . as_millis ( )
100
- . try_into ( )
101
- . expect ( "Provided timeout does not fit in a c_int" ) ;
94
+ self . enter ( ) ;
95
+ let res = self . iterate_unguarded ( timeout) ;
96
+ self . leave ( ) ;
102
97
103
- spa_interface_call_method ! (
104
- & mut iface as * mut spa_sys:: spa_interface,
105
- spa_sys:: spa_loop_control_methods,
106
- iterate,
107
- timeout
108
- )
98
+ res
109
99
}
110
100
}
111
101
102
+ /// A variant of [`iterate()`](`Self::iterate()`) that does not call [`Self::enter()`] and [`Self::leave()`] on the loop.
103
+ ///
104
+ /// # Safety
105
+ /// Before calling this, [`Self::enter()`] must be called, and [`Self::leave()`] must be called afterwards.
106
+ pub unsafe fn iterate_unguarded ( & self , timeout : std:: time:: Duration ) -> i32 {
107
+ let mut iface = self . as_raw ( ) . control . as_ref ( ) . unwrap ( ) . iface ;
108
+
109
+ let timeout: c_int = timeout
110
+ . as_millis ( )
111
+ . try_into ( )
112
+ . expect ( "Provided timeout does not fit in a c_int" ) ;
113
+
114
+ spa_interface_call_method ! (
115
+ & mut iface as * mut spa_sys:: spa_interface,
116
+ spa_sys:: spa_loop_control_methods,
117
+ iterate,
118
+ timeout
119
+ )
120
+ }
121
+
112
122
/// Register some type of IO object with a callback that is called when reading/writing on the IO object
113
123
/// is available.
114
124
///
0 commit comments