pub trait Transform<In>:
Send
+ Sync
+ 'static {
type Output: Send + 'static;
// Required method
fn apply(&self, index: usize, item: In) -> Result<Self::Output>;
// Provided methods
fn is_identity(&self) -> bool { ... }
fn then<T>(self, next: T) -> Then<Self, T>
where Self: Sized,
T: Transform<Self::Output> { ... }
fn apply_batch(
&self,
indices: &[usize],
items: Vec<In>,
) -> Result<Vec<Self::Output>> { ... }
}Expand description
A synchronous, CPU-side step the loader applies to every item, such as decoding bytes into
an image. Set with DataLoaderBuilder::transform.
A dataset only fetches; the transform runs on the loader’s threads, with the index of the
item: right after get on the worker for a Dataset, on the loader’s
rayon pool once the item is fetched for an AsyncDataset (feature async).
Closures Fn(usize, In) -> Result<Out> are transforms; then chains two. A
closure passed where a transform is expected needs its types written out, as Rust does not
infer them through this trait:
use dataloader_rs::{Transform, error::Result};
let double = |_: usize, x: u64| -> Result<u64> { Ok(x * 2) };
let label = |index: usize, x: u64| -> Result<String> { Ok(format!("{index}:{x}")) };
assert_eq!(double.then(label).apply(7, 21).unwrap(), "7:42");Required Associated Types§
Required Methods§
Provided Methods§
Sourcefn is_identity(&self) -> bool
fn is_identity(&self) -> bool
Whether apply returns its input as is: the async path then applies
it where the item is fetched instead of handing it to the pool. Default: false.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".