pub trait RasterElement:
Copy
+ Default
+ Send
+ Sync
+ 'static
+ Sealed {
type Bytes: AsRef<[u8]> + AsMut<[u8]> + Copy + Default + for<'a> TryFrom<&'a [u8]>;
const DATA_TYPE: RasterDataType;
const KIND: RasterElementKind;
const SIZE: usize = _;
// Required methods
fn from_ne_bytes(bytes: Self::Bytes) -> Self;
fn to_ne_bytes(self) -> Self::Bytes;
fn to_raster_f64(self) -> f64;
fn to_raster_i128(self) -> i128;
fn from_raster_f64(value: f64) -> Self;
fn from_raster_f64_truncating(value: f64) -> Self;
fn from_raster_i128(value: i128) -> Self;
// Provided method
fn from_ne_slice(bytes: &[u8]) -> Self { ... }
}Expand description
A Rust primitive that can appear as a raster sample.
Implemented for exactly the ten scalar raster types — u8, i8, u16,
i16, u32, i32, u64, i64, f32 and f64 — and sealed, so the set
cannot grow outside this crate.
§Thread safety
Every implementor is a primitive scalar, so Send + Sync is part of the
contract. Generic code bounded on RasterElement may therefore split a
&mut [T] across worker threads without restating the bound — which is what
lets the GeoTIFF driver run its typed band reads on rayon workers, not just
the raw-byte ones. Because the trait is sealed the guarantee costs nothing:
no out-of-crate type can ever weaken it.
Use it as a bound to write code that is generic over the pixel type:
use oxigeo_core::buffer::{RasterElement, convert_raw_into};
use oxigeo_core::error::Result;
use oxigeo_core::types::RasterDataType;
fn decode<T: RasterElement>(raw: &[u8], src: RasterDataType, dst: &mut [T]) -> Result<()> {
convert_raw_into(raw, src, dst)
}
let mut out = [0i32; 2];
decode(&[1u8, 2], RasterDataType::UInt8, &mut out)?;
assert_eq!(out, [1, 2]);
assert_eq!(i32::DATA_TYPE, RasterDataType::Int32);Required Associated Constants§
Sourceconst DATA_TYPE: RasterDataType
const DATA_TYPE: RasterDataType
The RasterDataType this Rust type stores.
Sourceconst KIND: RasterElementKind
const KIND: RasterElementKind
Whether this type is an integer or a float.
Provided Associated Constants§
Required Associated Types§
Required Methods§
Sourcefn from_ne_bytes(bytes: Self::Bytes) -> Self
fn from_ne_bytes(bytes: Self::Bytes) -> Self
Decodes a sample from its native-endian byte array.
Sourcefn to_ne_bytes(self) -> Self::Bytes
fn to_ne_bytes(self) -> Self::Bytes
Encodes a sample into its native-endian byte array.
Sourcefn to_raster_f64(self) -> f64
fn to_raster_f64(self) -> f64
Widens the sample to f64.
Exact for every type except u64/i64 magnitudes beyond
253, which are rounded to the nearest representable f64.
Sourcefn to_raster_i128(self) -> i128
fn to_raster_i128(self) -> i128
Widens the sample to i128.
Exact for every integer type. For floats the value is rounded half away
from zero and saturated at the i128 bounds; NaN becomes 0.
Sourcefn from_raster_f64(value: f64) -> Self
fn from_raster_f64(value: f64) -> Self
Narrows an f64 to this type, saturating at the type bounds and
rounding halves away from zero.
NaN becomes 0 for integer destinations; float destinations keep
NaN/±∞ as-is.
Sourcefn from_raster_f64_truncating(value: f64) -> Self
fn from_raster_f64_truncating(value: f64) -> Self
Narrows an f64 to this type, saturating at the type bounds and
truncating towards zero (C-style (int)x).
Identical to RasterElement::from_raster_f64 for float destinations.
Sourcefn from_raster_i128(value: i128) -> Self
fn from_raster_i128(value: i128) -> Self
Narrows an i128 to this type, saturating at the type bounds.
Exact for integer destinations. Float destinations round to nearest.
Provided Methods§
Sourcefn from_ne_slice(bytes: &[u8]) -> Self
fn from_ne_slice(bytes: &[u8]) -> Self
Decodes a sample from the first SIZE bytes of
bytes (native endian).
Longer slices are truncated — this is what makes complex sources work,
where each stride holds [real, imaginary] and only the real component
is read. Shorter slices yield Default::default instead of panicking.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".