Skip to main content

Transform

Trait Transform 

Source
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§

Source

type Output: Send + 'static

The transformed item.

Required Methods§

Source

fn apply(&self, index: usize, item: In) -> Result<Self::Output>

Transform item, the item at index. Called concurrently from several threads.

Provided Methods§

Source

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.

Source

fn then<T>(self, next: T) -> Then<Self, T>
where Self: Sized, T: Transform<Self::Output>,

Apply self, then next to its output.

Source

fn apply_batch( &self, indices: &[usize], items: Vec<In>, ) -> Result<Vec<Self::Output>>

Transform the items of a batch, items[i] being the item at indices[i]; both have the same length. Default: apply to each item in order, up to the first error. Identity returns the items as they are, without a pass over them.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl<In, A, B> Transform<In> for Then<A, B>
where A: Transform<In>, B: Transform<A::Output>,

Source§

type Output = <B as Transform<<A as Transform<In>>::Output>>::Output

Source§

impl<In, Out, F> Transform<In> for F
where F: Fn(usize, In) -> Result<Out> + Send + Sync + 'static, Out: Send + 'static,

Source§

type Output = Out

Source§

impl<In: Send + 'static> Transform<In> for Identity

Source§

type Output = In