ref_ops/
lib.rs

1#![no_std]
2
3//! An escape hatch for implementing `ops` traits for references to newtypes.
4//!
5//! As of Rust 1.73.0, the following code does not compile:
6//! ```compile_fail
7//! use core::ops::Neg;
8//!
9//! struct A<T>(T);
10//!
11//! impl<'a, T> Neg for &'a A<T>
12//! where
13//!     &'a T: Neg,
14//! {
15//!     type Output = A<<&'a T as Neg>::Output>;
16//!
17//!     fn neg(self) -> Self::Output {
18//!         A(-&self.0)
19//!     }
20//! }
21//!
22//! fn f<T>(a: T)
23//! where
24//!     for<'a> &'a T: Neg,
25//! {
26//!     let minus_a = -&a;
27//!
28//!     // to do something with `a` and `minus_a`
29//! }
30//!
31//! fn g<T>(a: T)
32//! where
33//!     for<'a> &'a T: Neg,
34//! {
35//!     f(a);
36//! }
37//! ```
38//! but the following code does:
39//! ```
40//! use core::ops::Neg;
41//! use ref_ops::RefNeg;
42//!
43//! struct A<T>(T);
44//!
45//! impl<T> Neg for &A<T>
46//! where
47//!     T: RefNeg,
48//! {
49//!     type Output = A<T::Output>;
50//!
51//!     fn neg(self) -> Self::Output {
52//!         A(self.0.ref_neg())
53//!     }
54//! }
55//!
56//! fn f<T>(a: T)
57//! where
58//!     for<'a> &'a T: Neg,
59//! {
60//!     let minus_a = -&a;
61//!
62//!     // to do something with `a` and `minus_a`
63//! }
64//!
65//! fn g<T>(a: T)
66//! where
67//!     for<'a> &'a T: Neg,
68//! {
69//!     f(a);
70//! }
71//! ```
72
73mod ref_unary;
74pub use ref_unary::*;
75
76mod ref_mut_unary;
77pub use ref_mut_unary::*;
78
79mod ref_binary;
80pub use ref_binary::*;
81
82mod ref_mut_binary;
83pub use ref_mut_binary::*;