Trait Accessor

Source
pub trait Accessor<R>: Clone {
    type Value;

    // Required method
    fn value<'a>(&self, data: &'a R) -> &'a Self::Value;
}
Available on crate feature sort only.
Expand description

Return a particular member of an instance by reference.

When Accessor is combined with the marker trait ReferenceField, then Field is automatically derived.

Example:

use django_query::sorting::{Accessor, Field, Compare, ReferenceField};
use core::cmp::Ordering;

struct Foo {
  a: i32
}

#[derive(Clone)]
struct FooA;

impl Accessor<Foo> for FooA {
    type Value = i32;
    fn value<'a>(&self, data: &'a Foo) -> &'a i32 {
       &data.a
    }
}

impl ReferenceField for FooA {}

let f_a = FooA;
let foo1 = Foo { a: 20 };
let foo2 = Foo { a: 10 };
assert_eq!(f_a.value(&foo1), &20i32);
assert_eq!(f_a.apply_sorter(&Compare, &foo1, &foo2), Ordering::Greater);

Required Associated Types§

Required Methods§

Source

fn value<'a>(&self, data: &'a R) -> &'a Self::Value

Return a reference to a member of data

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§