use alloc::borrow::ToOwned;
use alloc::boxed::Box;
use alloc::rc::Rc;
use alloc::string::String;
use alloc::vec::Vec;
use core::fmt::{Debug, Display};
use core::ops::Deref;
#[derive(Default, Clone, Eq, PartialEq, Hash)]
#[must_use]
#[repr(transparent)]
pub struct AudioSources(Box<[AudioSource]>);
impl Deref for AudioSources {
type Target = [AudioSource];
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl Debug for AudioSources {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
for (i, source) in self.0.iter().enumerate() {
if i > 0 {
write!(f, ", ")?;
}
write!(f, "{source:?}")?;
}
Ok(())
}
}
impl Display for AudioSources {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
for (i, source) in self.0.iter().enumerate() {
if i > 0 {
write!(f, ", ")?;
}
write!(f, "{source}")?;
}
Ok(())
}
}
impl FromIterator<AudioSource> for AudioSources {
fn from_iter<T: IntoIterator<Item = AudioSource>>(iter: T) -> Self {
Self(iter.into_iter().collect())
}
}
impl From<&[AudioSource]> for AudioSources {
fn from(slice: &[AudioSource]) -> Self {
Self(slice.to_owned().into_boxed_slice())
}
}
impl From<Box<[AudioSource]>> for AudioSources {
fn from(boxed: Box<[AudioSource]>) -> Self {
Self(boxed)
}
}
impl From<Vec<AudioSource>> for AudioSources {
fn from(vec: Vec<AudioSource>) -> Self {
Self(vec.into_boxed_slice())
}
}
impl<'a> IntoIterator for &'a AudioSources {
type Item = &'a AudioSource;
type IntoIter = core::slice::Iter<'a, AudioSource>;
fn into_iter(self) -> Self::IntoIter {
self.0.iter()
}
}
impl IntoIterator for AudioSources {
type Item = AudioSource;
type IntoIter = alloc::vec::IntoIter<AudioSource>;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}
#[derive(Default, Clone, PartialEq, Eq, Hash)]
#[must_use]
pub struct AudioSource {
pub id: SourceId,
pub name: SourceName,
}
impl Debug for AudioSource {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "[{}] {}", self.id, self.name)
}
}
impl Display for AudioSource {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{}", self.name)
}
}
#[derive(Default, Clone, Debug, Eq, PartialEq, Hash)]
#[repr(transparent)]
pub struct SourceId(Box<[u32]>);
impl AsRef<[u32]> for SourceId {
fn as_ref(&self) -> &[u32] {
&self.0
}
}
impl Deref for SourceId {
type Target = [u32];
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl Display for SourceId {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
for (i, id) in self.0.iter().enumerate() {
if i > 0 {
write!(f, ", ")?;
}
write!(f, "{id}")?;
}
Ok(())
}
}
impl From<u32> for SourceId {
fn from(value: u32) -> Self {
Self(Box::new([value]))
}
}
impl From<&[u32]> for SourceId {
fn from(values: &[u32]) -> Self {
Self(Box::from(Vec::from(values)))
}
}
impl From<Vec<u32>> for SourceId {
fn from(value: Vec<u32>) -> Self {
Self(value.into_boxed_slice())
}
}
#[derive(Default, Clone, Debug, Eq, PartialEq, Hash)]
#[repr(transparent)]
pub struct SourceName(Box<str>);
impl AsRef<str> for SourceName {
fn as_ref(&self) -> &str {
&self.0
}
}
impl Deref for SourceName {
type Target = str;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl Display for SourceName {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{}", self.0)
}
}
impl From<&str> for SourceName {
fn from(value: &str) -> Self {
Self(value.into())
}
}
impl From<Rc<str>> for SourceName {
fn from(value: Rc<str>) -> Self {
Self(value.as_ref().into())
}
}
impl From<String> for SourceName {
fn from(value: String) -> Self {
Self(value.into_boxed_str())
}
}