fmod/core/channel_group/
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 channel_management;
13mod general;
14mod group_management;
15
16#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
17#[repr(transparent)] // so we can transmute between types
18pub struct ChannelGroup {
19    pub(crate) inner: *mut FMOD_CHANNELGROUP,
20}
21
22unsafe impl Send for ChannelGroup {}
23unsafe impl Sync for ChannelGroup {}
24
25impl From<*mut FMOD_CHANNELGROUP> for ChannelGroup {
26    fn from(value: *mut FMOD_CHANNELGROUP) -> Self {
27        ChannelGroup { inner: value }
28    }
29}
30
31impl From<ChannelGroup> for *mut FMOD_CHANNELGROUP {
32    fn from(value: ChannelGroup) -> Self {
33        value.inner
34    }
35}
36
37impl Deref for ChannelGroup {
38    type Target = ChannelControl;
39
40    fn deref(&self) -> &Self::Target {
41        #[cfg(debug_assertions)]
42        unsafe {
43            // perform a debug check to ensure that the the cast results in the same pointer
44            let control = FMOD_ChannelGroup_CastToControl(self.inner);
45            assert_eq!(
46                control as usize, self.inner as usize,
47                "ChannelControl cast was not equivalent! THIS IS A MAJOR BUG. PLEASE REPORT THIS!"
48            );
49        }
50        // channelcontrol has the same layout as channel, and if the assumption in channel_control.rs is correct,
51        // this is cast is safe.
52        unsafe { &*std::ptr::from_ref(self).cast::<ChannelControl>() }
53    }
54}