looklook 0.9.0

Descriptive signal synthesiser.
// Copyright 2026 Gabriel Bjørnager Jensen.
//
// This file is part of LOOKLOOK.
//
// LOOKLOOK is free software: you can redistribute it and/or modify it under the
// terms of the GNU Affero General Public License as published by the Free Software
// Foundation, either version 3 of the License, or (at your option) any later ver-
// sion.
//
// LOOKLOOK is distributed in the hope that it will be useful, but WITHOUT ANY WAR-
// RANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
// PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License along
// with LOOKLOOK. If not, see <https://www.gnu.org/licenses/>.

//! The [`InstructionLen`] type.

use std::cmp::Ordering;
use std::fmt::{self, Debug, Display, Formatter};
use std::mem::transmute;

/// An instruction length.
#[repr(transparent)]
#[derive(Copy)]
#[derive_const(Clone)]
pub struct InstructionLen(pattern_type!(usize is 1..=6));

impl InstructionLen {
	/// The minimum, possible instruction length.
	pub const MIN: Self = Self::new(1).unwrap();

	/// The maximum, possible instruction length.
	pub const MAX: Self = Self::new(6).unwrap();

	/// Constructs a new instruction length.
	#[inline]
	#[must_use]
	pub const fn new(value: usize) -> Option<Self> {
		if let 1..=6 = value {
			Some(unsafe { transmute::<usize, Self>(value) })
		} else {
			None
		}
	}

	/// Retrieves the instruction length as a [`usize`] value.
	#[inline(always)]
	#[must_use]
	pub const fn get(self) -> usize {
		unsafe { transmute::<Self, usize>(self) }
	}
}

impl Debug for InstructionLen {
	#[inline]
	fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
		Debug::fmt(&self.get(), f)
	}
}

impl Display for InstructionLen {
	#[inline]
	fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
		Display::fmt(&self.get(), f)
	}
}

const impl Eq for InstructionLen {}

const impl From<InstructionLen> for usize {
	#[inline(always)]
	fn from(value: InstructionLen) -> Self {
		value.get()
	}
}

const impl Ord for InstructionLen {
	#[inline]
	fn cmp(&self, other: &Self) -> Ordering {
		self.get().cmp(&other.get())
	}
}

const impl PartialEq for InstructionLen {
	#[inline]
	fn eq(&self, other: &Self) -> bool {
		self.get().eq(&other.get())
	}
}

const impl PartialOrd for InstructionLen {
	#[inline]
	fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
		Some(self.cmp(other))
	}
}