mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-27 09:14:20 +00:00
33 lines
628 B
Rust
33 lines
628 B
Rust
// run-pass
|
|
use std::ops::{Deref, DerefMut};
|
|
|
|
struct CheckedDeref<T, F> {
|
|
value: T,
|
|
check: F
|
|
}
|
|
|
|
impl<F: Fn(&T) -> bool, T> Deref for CheckedDeref<T, F> {
|
|
type Target = T;
|
|
fn deref(&self) -> &T {
|
|
assert!((self.check)(&self.value));
|
|
&self.value
|
|
}
|
|
}
|
|
|
|
impl<F: Fn(&T) -> bool, T> DerefMut for CheckedDeref<T, F> {
|
|
fn deref_mut(&mut self) -> &mut T {
|
|
assert!((self.check)(&self.value));
|
|
&mut self.value
|
|
}
|
|
}
|
|
|
|
|
|
fn main() {
|
|
let mut v = CheckedDeref {
|
|
value: vec![0],
|
|
check: |v: &Vec<_>| !v.is_empty()
|
|
};
|
|
v.push(1);
|
|
assert_eq!(*v, vec![0, 1]);
|
|
}
|