Trait AssociatedValue

Source
pub trait AssociatedValue {
    type T: 'static + Clone;

    // Required method
    unsafe fn __local_key() -> &'static LocalKey<Cell<Self::T>>;

    // Provided methods
    fn get() -> Self::T { ... }
    fn set(x: Self::T) { ... }
    fn replace(x: Self::T) -> Self::T { ... }
    fn with<F, R>(f: F) -> R
       where F: FnOnce(&Self::T) -> R { ... }
    fn modify<F, R>(f: F) -> R
       where F: FnOnce(&mut Self::T) -> R { ... }
}
Expand description

Trait for a modifiable value associated with a type.

Required Associated Types§

Source

type T: 'static + Clone

Type of value.

Required Methods§

Source

unsafe fn __local_key() -> &'static LocalKey<Cell<Self::T>>

Provided Methods§

Source

fn get() -> Self::T

Source

fn set(x: Self::T)

Source

fn replace(x: Self::T) -> Self::T

Source

fn with<F, R>(f: F) -> R
where F: FnOnce(&Self::T) -> R,

Examples found in repository?
crates/competitive/src/tools/associated_value.rs (line 8)
7    fn get() -> Self::T {
8        Self::with(Clone::clone)
9    }
More examples
Hide additional examples
crates/competitive/src/math/fast_fourier_transform.rs (lines 133-146)
130pub fn fft(a: &mut [Complex<f64>]) {
131    let n = a.len();
132    RotateCache::ensure(n / 2);
133    RotateCache::with(|cache| {
134        let mut v = n / 2;
135        while v > 0 {
136            for (a, wj) in a.chunks_exact_mut(v << 1).zip(cache) {
137                let (l, r) = a.split_at_mut(v);
138                for (x, y) in l.iter_mut().zip(r) {
139                    let ajv = wj * *y;
140                    *y = *x - ajv;
141                    *x += ajv;
142                }
143            }
144            v >>= 1;
145        }
146    });
147}
148
149pub fn ifft(a: &mut [Complex<f64>]) {
150    let n = a.len();
151    RotateCache::ensure(n / 2);
152    RotateCache::with(|cache| {
153        let mut v = 1;
154        while v < n {
155            for (a, wj) in a
156                .chunks_exact_mut(v << 1)
157                .zip(cache.iter().map(|wj| wj.conjugate()))
158            {
159                let (l, r) = a.split_at_mut(v);
160                for (x, y) in l.iter_mut().zip(r) {
161                    let ajv = *x - *y;
162                    *x += *y;
163                    *y = wj * ajv;
164                }
165            }
166            v <<= 1;
167        }
168    });
169}
Source

fn modify<F, R>(f: F) -> R
where F: FnOnce(&mut Self::T) -> R,

Examples found in repository?
crates/competitive/src/math/fast_fourier_transform.rs (lines 9-32)
7    fn ensure(n: usize) {
8        assert_eq!(n.count_ones(), 1, "call with power of two but {}", n);
9        Self::modify(|cache| {
10            let mut m = cache.len();
11            assert!(
12                m.count_ones() <= 1,
13                "length might be power of two but {}",
14                m
15            );
16            if m >= n {
17                return;
18            }
19            cache.reserve_exact(n - m);
20            if cache.is_empty() {
21                cache.push(Complex::one());
22                m += 1;
23            }
24            while m < n {
25                let p = Complex::primitive_nth_root_of_unity(-((m * 4) as f64));
26                for i in 0..m {
27                    cache.push(cache[i] * p);
28                }
29                m <<= 1;
30            }
31            assert_eq!(cache.len(), n);
32        });
33    }

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§