diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
commit | 2aa4a82499d4becd2284cdb482213d541b8804dd (patch) | |
tree | b80bf8bf13c3766139fbacc530efd0dd9d54394c /third_party/rust/image/src/traits.rs | |
parent | Initial commit. (diff) | |
download | firefox-upstream.tar.xz firefox-upstream.zip |
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/image/src/traits.rs')
-rw-r--r-- | third_party/rust/image/src/traits.rs | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/third_party/rust/image/src/traits.rs b/third_party/rust/image/src/traits.rs new file mode 100644 index 0000000000..6e558d4a13 --- /dev/null +++ b/third_party/rust/image/src/traits.rs @@ -0,0 +1,75 @@ +//! This module provides useful traits that were deprecated in rust + +// Note copied from the stdlib under MIT license + +use num_traits::{Bounded, Num, NumCast}; +use std::ops::AddAssign; + +/// Types which are safe to treat as an immutable byte slice in a pixel layout +/// for image encoding. +pub trait EncodableLayout: seals::EncodableLayout { + /// Get the bytes of this value. + fn as_bytes(&self) -> &[u8]; +} + +impl EncodableLayout for [u8] { + fn as_bytes(&self) -> &[u8] { + bytemuck::cast_slice(self) + } +} + +impl EncodableLayout for [u16] { + fn as_bytes(&self) -> &[u8] { + bytemuck::cast_slice(self) + } +} + +/// Primitive trait from old stdlib +pub trait Primitive: Copy + NumCast + Num + PartialOrd<Self> + Clone + Bounded {} + +impl Primitive for usize {} +impl Primitive for u8 {} +impl Primitive for u16 {} +impl Primitive for u32 {} +impl Primitive for u64 {} +impl Primitive for isize {} +impl Primitive for i8 {} +impl Primitive for i16 {} +impl Primitive for i32 {} +impl Primitive for i64 {} +impl Primitive for f32 {} +impl Primitive for f64 {} + +/// An Enlargable::Larger value should be enough to calculate +/// the sum (average) of a few hundred or thousand Enlargeable values. +pub trait Enlargeable: Sized + Bounded + NumCast { + type Larger: Primitive + AddAssign + 'static; + + fn clamp_from(n: Self::Larger) -> Self { + // Note: Only unsigned value types supported. + if n > NumCast::from(Self::max_value()).unwrap() { + Self::max_value() + } else { + NumCast::from(n).unwrap() + } + } +} + +impl Enlargeable for u8 { + type Larger = u32; +} +impl Enlargeable for u16 { + type Larger = u32; +} +impl Enlargeable for u32 { + type Larger = u64; +} + + +/// Private module for supertraits of sealed traits. +mod seals { + pub trait EncodableLayout {} + + impl EncodableLayout for [u8] {} + impl EncodableLayout for [u16] {} +} |