fmod/core/channel/
mod.rs

1// Copyright (c) 2024 Lily Lyons
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at https://mozilla.org/MPL/2.0/.
6
7use fmod_sys::*;
8use std::ops::Deref;
9
10use crate::ChannelControl;
11
12mod information;
13mod playback_control;
14
15#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
16#[repr(transparent)] // so we can transmute between types
17pub struct Channel {
18    pub(crate) inner: *mut FMOD_CHANNEL,
19}
20
21unsafe impl Send for Channel {}
22unsafe impl Sync for Channel {}
23
24impl From<*mut FMOD_CHANNEL> for Channel {
25    fn from(value: *mut FMOD_CHANNEL) -> Self {
26        Channel { inner: value }
27    }
28}
29
30impl From<Channel> for *mut FMOD_CHANNEL {
31    fn from(value: Channel) -> Self {
32        value.inner
33    }
34}
35
36impl Deref for Channel {
37    type Target = ChannelControl;
38
39    fn deref(&self) -> &Self::Target {
40        #[cfg(debug_assertions)]
41        unsafe {
42            // perform a debug check to ensure that the the cast results in the same pointer
43            let control = FMOD_Channel_CastToControl(self.inner);
44            assert_eq!(
45                control as usize, self.inner as usize,
46                "ChannelControl cast was not equivalent! THIS IS A MAJOR BUG. PLEASE REPORT THIS!"
47            );
48        }
49        // channelcontrol has the same layout as channel, and if the assumption in channel_control.rs is correct,
50        // this is cast is safe.
51        unsafe { &*std::ptr::from_ref(self).cast::<ChannelControl>() }
52    }
53}