たとえば、すべてが読み取り専用参照を格納する構造体があります。
struct Pt { x : f32, y : f32, }
struct Tr<'a> { a : &'a Pt }
私がしたいimpl Eq
ためにTr
、下にあるかどうかをテストするためにa
正確に同じへの参照Pt
:
let trBase1 = Pt::new(0.0, 0.0);
let trBase2 = Pt::new(0.0, 0.0);
assert!(trBase1 == trBase2); // ok.
let tr1 = Tr::new(&trBase1);
let tr2 = Tr::new(&trBase2);
let tr3 = Tr::new(&trBase1);
assert!(tr1 == tr3); // ok.
assert!(tr1.a == te2.a); // ok. Using Eq for Pt that compare values.
assert!(tr1 != tr2); // panicked! Not intended.
だから今私は持っています
impl<'a> PartialEq for Tr<'a> {
fn eq(&self, v : &Tr<'a>) -> bool {
// self.a == v.a // doesn't work.
}
}
私は何を書くべきですか?
を使用std::ptr::eq
して、2つのポインタのアドレスを比較できます。参照(&T
または&mut T
)は*const T
、この関数に供給されると、基になるポインター()に自動的に強制されます。もちろん、可変参照は常に排他的参照であるため、可変参照が別の参照と同じアドレスを持つことは意味がありませんが、それでも強制的に*const T
。
// This derive will use the equality of the underlying fields
#[derive(PartialEq)]
struct Pt {
x: f32,
y: f32,
}
impl Pt {
fn new(x: f32, y: f32) -> Self {
Self { x, y }
}
}
struct Tr<'a> {
a: &'a Pt,
}
impl<'a> Tr<'a> {
fn new(a: &'a Pt) -> Self {
Self { a }
}
}
// Here we use std::ptr::eq to compare the *addresses* of `self.a` and `other.a`
impl<'a> PartialEq for Tr<'a> {
fn eq(&self, other: &Tr<'a>) -> bool {
std::ptr::eq(self.a, other.a)
}
}
fn main() {
let tr_base1 = Pt::new(0.0, 0.0);
let tr_base2 = Pt::new(0.0, 0.0);
assert!(tr_base1 == tr_base2);
let tr1 = Tr::new(&tr_base1);
let tr2 = Tr::new(&tr_base2);
let tr3 = Tr::new(&tr_base1);
assert!(tr1 == tr3);
assert!(tr1.a == tr2.a);
assert!(tr1 != tr2);
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加