use core::cell::Cell;
pub const trait SetCellToZero {
fn set_zero(&self);
}
impl<T: crate::math::ConstZero> SetCellToZero for core::cell::Cell<T> {
fn set_zero(&self) {
self.replace(T::ZERO);
}
}
pub const trait CellIntOps<T> {
fn add(&self, rhs: T);
fn sub(&self, rhs: T);
fn mul(&self, rhs: T);
fn div(&self, rhs: T);
}
pub const trait CellIntReturnOps<T> {
fn added(&self, rhs: T) -> T;
fn subtracted(&self, rhs: T) -> T;
fn multiplied(&self, rhs: T) -> T;
fn divided(&self, rhs: T) -> T;
}
pub const trait CellIntSaturatedOps<T> {
fn saturating_add(&self, rhs: T);
fn saturating_sub(&self, rhs: T);
fn saturating_mul(&self, rhs: T);
}
pub const trait CellIntSaturatedReturnOps<T> {
fn saturating_added(&self, rhs: T) -> T;
fn saturating_subtracted(&self, rhs: T) -> T;
fn saturating_multiplied(&self, rhs: T) -> T;
}
impl<T> const CellIntOps<T> for Cell<T>
where
T: Copy
+ [const] core::ops::Add<Output = T>
+ [const] core::ops::Sub<Output = T>
+ [const] core::ops::Mul<Output = T>
+ [const] core::ops::Div<Output = T>,
{
fn add(&self, rhs: T) {
self.replace(self.get() + rhs);
}
fn sub(&self, rhs: T) {
self.replace(self.get() - rhs);
}
fn mul(&self, rhs: T) {
self.replace(self.get() * rhs);
}
fn div(&self, rhs: T) {
self.replace(self.get() / rhs);
}
}
impl<T> CellIntReturnOps<T> for Cell<T>
where
T: Copy
+ core::ops::Add<Output = T>
+ core::ops::Sub<Output = T>
+ core::ops::Mul<Output = T>
+ core::ops::Div<Output = T>,
{
fn added(&self, rhs: T) -> T {
let result = self.get() + rhs;
self.replace(result);
result
}
fn subtracted(&self, rhs: T) -> T {
let result = self.get() - rhs;
self.replace(result);
result
}
fn multiplied(&self, rhs: T) -> T {
let result = self.get() * rhs;
self.replace(result);
result
}
fn divided(&self, rhs: T) -> T {
let result = self.get() / rhs;
self.replace(result);
result
}
}
impl<T> const CellIntSaturatedOps<T> for Cell<T>
where
T: Copy
+ [const] crate::extensions::SaturatingAdd<T, Output = T>
+ [const] crate::extensions::SaturatingSub<T, Output = T>
+ [const] crate::extensions::SaturatingMul<T, Output = T>,
{
fn saturating_add(&self, rhs: T) {
self.replace(self.get().saturating_add(rhs));
}
fn saturating_sub(&self, rhs: T) {
self.replace(self.get().saturating_sub(rhs));
}
fn saturating_mul(&self, rhs: T) {
self.replace(self.get().saturating_mul(rhs));
}
}
impl<T> const CellIntSaturatedReturnOps<T> for Cell<T>
where
T: Copy
+ [const] crate::extensions::SaturatingAdd<T, Output = T>
+ [const] crate::extensions::SaturatingSub<T, Output = T>
+ [const] crate::extensions::SaturatingMul<T, Output = T>,
{
fn saturating_added(&self, rhs: T) -> T {
self.get().saturating_add(rhs)
}
fn saturating_subtracted(&self, rhs: T) -> T {
self.get().saturating_sub(rhs)
}
fn saturating_multiplied(&self, rhs: T) -> T {
self.get().saturating_mul(rhs)
}
}
pub const trait CellIntClampingOps<T> {
fn min(&self, rhs: T);
fn max(&self, rhs: T);
fn clamp(&self, min: T, max: T);
}
pub const trait CellIntClampingReturnOps<T> {
fn minned(&self, rhs: T) -> T;
fn maxed(&self, rhs: T) -> T;
fn clamped(&self, min: T, max: T) -> T;
}
pub const trait CellIntSignedOps<T> {
fn abs(&self);
fn neg(&self);
fn sign(&self);
}
pub const trait CellIntSignedReturnOps<T> {
fn absed(&self) -> T;
fn neged(&self) -> T;
fn sign(&self) -> T;
}
pub const trait CellIntSaturatedSignedOps<T> {
fn saturating_abs(&self);
fn saturating_neg(&self);
}
pub const trait CellIntSaturatedSignedReturnOps<T> {
fn saturating_absed(&self) -> T;
fn saturating_neged(&self) -> T;
}
impl<T> CellIntClampingOps<T> for Cell<T>
where
T: Copy + Ord,
{
fn min(&self, rhs: T) {
self.replace(self.get().min(rhs));
}
fn max(&self, rhs: T) {
self.replace(self.get().max(rhs));
}
fn clamp(&self, min: T, max: T) {
self.replace(self.get().clamp(min, max));
}
}
impl<T> CellIntClampingReturnOps<T> for Cell<T>
where
T: Copy + Ord,
{
fn minned(&self, rhs: T) -> T {
self.get().min(rhs)
}
fn maxed(&self, rhs: T) -> T {
self.get().max(rhs)
}
fn clamped(&self, min: T, max: T) -> T {
self.get().clamp(min, max)
}
}
impl<T> const CellIntSignedOps<T> for Cell<T>
where
T: Copy
+ [const] crate::extensions::Abs
+ [const] core::ops::Neg<Output = T>
+ [const] crate::extensions::Sign,
{
fn abs(&self) {
self.replace(self.get().abs());
}
fn neg(&self) {
self.replace(-self.get());
}
fn sign(&self) {
self.replace(self.get().sign());
}
}
impl<T> const CellIntSignedReturnOps<T> for Cell<T>
where
T: Copy
+ [const] crate::extensions::Abs
+ [const] core::ops::Neg<Output = T>
+ [const] crate::extensions::Sign,
{
fn absed(&self) -> T {
self.get().abs()
}
fn neged(&self) -> T {
-self.get()
}
fn sign(&self) -> T {
self.get().sign()
}
}
impl<T> const CellIntSaturatedSignedOps<T> for Cell<T>
where
T: Copy
+ [const] crate::extensions::SaturatingAbs<Output = T>
+ [const] crate::extensions::SaturatingNeg<Output = T>,
{
fn saturating_abs(&self) {
self.replace(self.get().saturating_abs());
}
fn saturating_neg(&self) {
self.replace(self.get().saturating_neg());
}
}
impl<T> const CellIntSaturatedSignedReturnOps<T> for Cell<T>
where
T: Copy
+ [const] crate::extensions::SaturatingAbs<Output = T>
+ [const] crate::extensions::SaturatingNeg<Output = T>,
{
fn saturating_absed(&self) -> T {
self.get().saturating_abs()
}
fn saturating_neged(&self) -> T {
self.get().saturating_neg()
}
}