recursive trait impls

This commit is contained in:
Breval Ferrari 2024-07-03 22:57:27 +02:00
parent f688dbe225
commit a1e2111e6d
No known key found for this signature in database
GPG key ID: CEAB625B75A836B2

View file

@ -1,66 +1,43 @@
use dasp_sample::{FromSample, Sample, U24, U48}; use dasp_sample::{FromSample, Sample, U24, U48};
pub trait AsSample<S> pub trait AsSample<F>
where where
S: Sample, Self: Sized,
{ {
fn as_sample(self) -> S; fn _prepare(self) -> F;
} fn as_sample<S: Sample + FromSample<F>>(self) -> S {
Sample::from_sample(self._prepare())
impl<S> AsSample<S> for u8
where
S: Sample + FromSample<u8>,
{
fn as_sample(self) -> S {
self.to_sample()
} }
} }
impl<S> AsSample<S> for [u8; 2] impl AsSample<u8> for u8 {
where fn _prepare(self) -> u8 {
S: Sample + FromSample<u16>, self
{
fn as_sample(self) -> S {
(((self[0] as u16) << 8) | self[1] as u16).to_sample()
} }
} }
impl<S> AsSample<S> for [u8; 3] impl AsSample<u16> for [u8; 2] {
where fn _prepare(self) -> u16 {
S: Sample + FromSample<U24>, ((self[0]._prepare() as u16) << 8) | self[1] as u16
{
fn as_sample(self) -> S {
(((U24::from(self[0])) << 16.into())
| (U24::from(self[1]) << 8.into())
| U24::from(self[2]))
.to_sample()
} }
} }
impl<S> AsSample<S> for [u8; 4] impl AsSample<U24> for [u8; 3] {
where fn _prepare(self) -> U24 {
S: Sample + FromSample<u32>, (U24::from([self[0], self[1]]._prepare()) << 8.into()) | U24::from(self[2])
{
fn as_sample(self) -> S {
(((self[0] as u32) << 24)
| ((self[1] as u32) << 16)
| ((self[2] as u32) << 8)
| self[3] as u32)
.to_sample()
} }
} }
impl<S> AsSample<S> for [u8; 6] impl AsSample<u32> for [u8; 4] {
where fn _prepare(self) -> u32 {
S: Sample + FromSample<U48>, (([self[0], self[1], self[2]]._prepare().inner() as u32) << 8) | self[3] as u32
{ }
fn as_sample(self) -> S { }
((U48::from(self[0]) << 40u8.into())
| (U48::from(self[1]) << 32u8.into()) impl AsSample<U48> for [u8; 6] {
| (U48::from(self[2]) << 24u8.into()) fn _prepare(self) -> U48 {
| (U48::from(self[3]) << 16u8.into()) ((U48::from([self[0], self[1], self[2], self[3]]._prepare()) << 16u8.into())
| (U48::from(self[4]) << 8u8.into()) | (U48::from(self[4]) << 8u8.into()))
| U48::from(self[3])) | U48::from(self[5])
.to_sample()
} }
} }