From c16c3327c35c0d6f5ebe63b96ff2ecf5205ca797 Mon Sep 17 00:00:00 2001 From: Ariel Ben-Yehuda Date: Wed, 27 May 2015 20:10:23 +0300 Subject: [PATCH] Implement raw pointer comparisons --- text/0000-raw-pointer-comparisons.md | 58 ++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 text/0000-raw-pointer-comparisons.md diff --git a/text/0000-raw-pointer-comparisons.md b/text/0000-raw-pointer-comparisons.md new file mode 100644 index 00000000000..5287c239951 --- /dev/null +++ b/text/0000-raw-pointer-comparisons.md @@ -0,0 +1,58 @@ +- Feature Name: raw-pointer-comparisons +- Start Date: 2015-05-27 +- RFC PR: (leave this empty) +- Rust Issue: (leave this empty) + +# Summary + +Allow equality, but not order, comparisons between fat raw pointers +of the same type. + +# Motivation + +Currently, fat raw pointers can't be compared via either PartialEq or +PartialOrd (currently this causes an ICE). It seems to me that a primitive +type like a fat raw pointer should implement equality in some way. + +However, there doesn't seem to be a sensible way to order raw fat pointers +unless we take vtable addresses into account, which is relatively weird. + +# Detailed design + +Implement PartialEq/Eq for fat raw pointers, defined as comparing both the +unsize-info and the address. This means that these are true: + +```Rust + &s as &fmt::Debug as *const _ == &s as &fmt::Debug as *const _ // of course + &s.first_field as &fmt::Debug as *const _ + != &s as &fmt::Debug as *const _ // these are *different* (one + // prints only the first field, + // the other prints all fields). +``` + +But +```Rust + &s.first_field as &fmt::Debug as *const _ as *const () == + &s as &fmt::Debug as *const _ as *const () // addresses are equal +``` + +# Drawbacks + +Order comparisons may be useful for putting fat raw pointers into +ordering-based data structures (e.g. BinaryTree). + +# Alternatives + +@nrc suggested to implement heterogeneous comparisons between all thin +raw pointers and all fat raw pointers. I don't like this because equality +between fat raw pointers of different traits is false most of the +time (unless one of the traits is a supertrait of the other and/or the +only difference is in free lifetimes), and anyway you can always compare +by casting both pointers to a common type. + +It is also possible to implement ordering too, either in unsize -> addr +lexicographic order or addr -> unsize lexicographic order. + +# Unresolved questions + +See Alternatives. \ No newline at end of file