use maudio_sys::ffi as sys;
pub type NodeFlagsRaw = sys::ma_node_flags;
#[repr(transparent)]
#[derive(Debug, PartialEq, Clone, Copy, Hash, Eq)]
struct NodeFlags(NodeFlagsRaw);
impl NodeFlags {
pub const NONE: Self = Self(0);
pub const PASSTHROUGH: Self = Self(sys::ma_node_flags_MA_NODE_FLAG_PASSTHROUGH);
pub const CONTINUOUS_PROCESSING: Self =
Self(sys::ma_node_flags_MA_NODE_FLAG_CONTINUOUS_PROCESSING);
pub const ALLOW_NULL_INPUT: Self = Self(sys::ma_node_flags_MA_NODE_FLAG_ALLOW_NULL_INPUT);
pub const DIFFERENT_PROCESSING_RATES: Self =
Self(sys::ma_node_flags_MA_NODE_FLAG_DIFFERENT_PROCESSING_RATES);
pub const SILENT_OUTPUT: Self = Self(sys::ma_node_flags_MA_NODE_FLAG_SILENT_OUTPUT);
#[inline]
#[allow(clippy::cast_sign_loss)]
#[allow(clippy::useless_conversion)]
#[allow(clippy::unnecessary_cast)]
pub fn bits(self) -> u32 {
self.0 as u32
}
#[inline]
pub const fn set(&mut self, other: Self, enabled: bool) {
if enabled {
self.0 |= other.0;
} else {
self.0 &= !other.0;
}
}
#[inline]
pub const fn from_bits(bits: u32) -> Self {
Self(bits as NodeFlagsRaw)
}
#[inline]
pub const fn contains(self, other: Self) -> bool {
(self.0 & other.0) == other.0
}
#[inline]
pub const fn intersects(self, other: Self) -> bool {
(self.0 & other.0) != 0
}
#[inline]
pub const fn is_none(self) -> bool {
self.0 == 0
}
#[inline]
pub const fn insert(&mut self, other: Self) {
self.0 |= other.0
}
#[inline]
pub(crate) const fn insert_bits(&mut self, other: &Self) {
self.0 |= other.0
}
#[inline]
pub const fn remove(&mut self, other: Self) {
self.0 &= !other.0
}
}
impl core::ops::BitOr for NodeFlags {
type Output = Self;
#[inline]
fn bitor(self, rhs: Self) -> Self {
Self(self.0 | rhs.0)
}
}
impl core::ops::BitOrAssign for NodeFlags {
#[inline]
fn bitor_assign(&mut self, rhs: Self) {
self.0 |= rhs.0;
}
}
impl core::ops::BitAnd for NodeFlags {
type Output = Self;
#[inline]
fn bitand(self, rhs: Self) -> Self {
Self(self.0 & rhs.0)
}
}
impl core::ops::BitAndAssign for NodeFlags {
#[inline]
fn bitand_assign(&mut self, rhs: Self) {
self.0 &= rhs.0;
}
}
impl core::ops::BitXor for NodeFlags {
type Output = Self;
#[inline]
fn bitxor(self, rhs: Self) -> Self {
Self(self.0 ^ rhs.0)
}
}
impl core::ops::Not for NodeFlags {
type Output = Self;
#[inline]
fn not(self) -> Self {
Self(!self.0)
}
}
impl From<NodeFlags> for u32 {
#[inline]
#[allow(clippy::cast_sign_loss)]
#[allow(clippy::useless_conversion)]
#[allow(clippy::unnecessary_cast)]
fn from(v: NodeFlags) -> u32 {
v.0 as u32
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn build_node_flags() {
let mut flag = NodeFlags::NONE;
flag.insert(NodeFlags::PASSTHROUGH);
assert!(flag == NodeFlags::PASSTHROUGH);
flag.remove(NodeFlags::PASSTHROUGH);
assert!(flag == NodeFlags::NONE);
flag.insert(NodeFlags::PASSTHROUGH);
flag.insert(NodeFlags::CONTINUOUS_PROCESSING);
assert!(flag == (NodeFlags::PASSTHROUGH | NodeFlags::CONTINUOUS_PROCESSING));
assert!(flag.contains(NodeFlags::PASSTHROUGH));
assert!(flag.contains(NodeFlags::CONTINUOUS_PROCESSING));
assert!(!flag.contains(NodeFlags::SILENT_OUTPUT));
flag.remove(NodeFlags::PASSTHROUGH);
assert!(flag == NodeFlags::CONTINUOUS_PROCESSING);
}
}