4
4
ffi:: audio:: * ,
5
5
system:: { Time , Vector3f } ,
6
6
} ,
7
- std:: { os:: raw:: c_void, panic, ptr:: NonNull } ,
7
+ std:: { marker :: PhantomData , os:: raw:: c_void, panic, ptr:: NonNull } ,
8
8
} ;
9
9
10
10
/// Trait for streamed audio sources.
@@ -29,7 +29,12 @@ pub trait SoundStream: Send {
29
29
#[ derive( Debug ) ]
30
30
pub struct SoundStreamPlayer < ' a , S : SoundStream + ' a > {
31
31
sf_sound_stream : NonNull < sfSoundStream > ,
32
- stream : & ' a mut S ,
32
+ /// We need to hold a raw pointer instead of a reference, since
33
+ /// we send it to another thread, violating `&mut` rules.
34
+ ///
35
+ /// Not sure if `NonNull` can be used to be honest. Not gonna risk it.
36
+ stream : * mut S ,
37
+ _borrow : PhantomData < & ' a mut S > ,
33
38
}
34
39
35
40
unsafe extern "C" fn get_data_callback < S : SoundStream > (
@@ -73,19 +78,22 @@ unsafe extern "C" fn seek_callback<S: SoundStream>(
73
78
impl < ' a , S : SoundStream > SoundStreamPlayer < ' a , S > {
74
79
/// Create a new `SoundStreamPlayer` with the specified [`SoundStream`].
75
80
pub fn new ( sound_stream : & ' a mut S ) -> Self {
76
- SoundStreamPlayer {
81
+ let channel_count = sound_stream. channel_count ( ) ;
82
+ let sample_rate = sound_stream. sample_rate ( ) ;
83
+ let sound_stream: * mut S = sound_stream;
84
+ Self {
77
85
sf_sound_stream : unsafe {
78
- let ptr: * mut S = sound_stream;
79
86
NonNull :: new ( sfSoundStream_create (
80
87
Some ( get_data_callback :: < S > ) ,
81
88
Some ( seek_callback :: < S > ) ,
82
- sound_stream . channel_count ( ) ,
83
- sound_stream . sample_rate ( ) ,
84
- ptr . cast ( ) ,
89
+ channel_count,
90
+ sample_rate,
91
+ sound_stream . cast ( ) ,
85
92
) )
86
93
. expect ( "Failed to create SoundStreamPlayer" )
87
94
} ,
88
95
stream : sound_stream,
96
+ _borrow : PhantomData ,
89
97
}
90
98
}
91
99
/// Start or resume playing the audio stream.
@@ -149,8 +157,8 @@ impl<'a, S: SoundStream> SoundStreamPlayer<'a, S> {
149
157
pub fn stop ( & mut self ) -> & mut S {
150
158
unsafe {
151
159
sfSoundStream_stop ( self . sf_sound_stream . as_ptr ( ) ) ;
160
+ & mut * self . stream
152
161
}
153
- self . stream
154
162
}
155
163
/// Get the current playing position, from the beginning of the stream
156
164
#[ must_use]
0 commit comments