Skip to content

Commit 2b74bff

Browse files
bors[bot]CAD97
andauthored
Merge #5
5: Make UnionBuilder proof always Copy r=CAD97 a=CAD97 bors: r+ Co-authored-by: CAD97 <cad97@cad97.com>
2 parents e52b6b1 + 237dd0d commit 2b74bff

1 file changed

Lines changed: 20 additions & 7 deletions

File tree

crates/ptr-union/src/lib.rs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,24 @@ fn unmask(ptr: ErasedPtr, mask: usize, value: usize) -> ErasedPtr {
5252
/// For that, you get all of the unsafe pointer-wrangling for pointer-sized pointer unions.
5353
///
5454
/// In the future, with sufficiently advanced const generics, it might be possible to avoid this.
55-
#[derive(Copy, Clone, Debug)]
5655
pub struct UnionBuilder<U> {
5756
private: PhantomData<U>,
5857
}
5958

59+
impl<U> Copy for UnionBuilder<U> {}
60+
impl<U> Clone for UnionBuilder<U> {
61+
fn clone(&self) -> Self {
62+
*self
63+
}
64+
}
65+
impl<U> fmt::Debug for UnionBuilder<U> {
66+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
67+
f.debug_tuple("UnionBuilder")
68+
.field(&format_args!("{}", core::any::type_name::<U>()))
69+
.finish()
70+
}
71+
}
72+
6073
impl<A, B> UnionBuilder<Union2<A, B>> {
6174
/// Assert that creating pointer unions of these two types is safe.
6275
///
@@ -160,15 +173,15 @@ pub struct Union4<A, B, C, D = NeverPtr> {
160173

161174
#[allow(missing_docs)]
162175
impl<A: ErasablePtr, B: ErasablePtr> UnionBuilder<Union2<A, B>> {
163-
pub fn a(&self, a: A) -> Union2<A, B> {
176+
pub fn a(self, a: A) -> Union2<A, B> {
164177
Union2 {
165178
raw: mask(A::erase(a), MASK_2, MASK_A),
166179
a: PhantomData,
167180
b: PhantomData,
168181
}
169182
}
170183

171-
pub fn b(&self, b: B) -> Union2<A, B> {
184+
pub fn b(self, b: B) -> Union2<A, B> {
172185
Union2 {
173186
raw: mask(B::erase(b), MASK_2, MASK_B),
174187
a: PhantomData,
@@ -181,7 +194,7 @@ impl<A: ErasablePtr, B: ErasablePtr> UnionBuilder<Union2<A, B>> {
181194
impl<A: ErasablePtr, B: ErasablePtr, C: ErasablePtr, D: ErasablePtr>
182195
UnionBuilder<Union4<A, B, C, D>>
183196
{
184-
pub fn a(&self, a: A) -> Union4<A, B, C, D> {
197+
pub fn a(self, a: A) -> Union4<A, B, C, D> {
185198
Union4 {
186199
raw: mask(A::erase(a), MASK_4, MASK_A),
187200
a: PhantomData,
@@ -191,7 +204,7 @@ impl<A: ErasablePtr, B: ErasablePtr, C: ErasablePtr, D: ErasablePtr>
191204
}
192205
}
193206

194-
pub fn b(&self, b: B) -> Union4<A, B, C, D> {
207+
pub fn b(self, b: B) -> Union4<A, B, C, D> {
195208
Union4 {
196209
raw: mask(B::erase(b), MASK_4, MASK_B),
197210
a: PhantomData,
@@ -201,7 +214,7 @@ impl<A: ErasablePtr, B: ErasablePtr, C: ErasablePtr, D: ErasablePtr>
201214
}
202215
}
203216

204-
pub fn c(&self, c: C) -> Union4<A, B, C, D> {
217+
pub fn c(self, c: C) -> Union4<A, B, C, D> {
205218
Union4 {
206219
raw: mask(C::erase(c), MASK_4, MASK_C),
207220
a: PhantomData,
@@ -211,7 +224,7 @@ impl<A: ErasablePtr, B: ErasablePtr, C: ErasablePtr, D: ErasablePtr>
211224
}
212225
}
213226

214-
pub fn d(&self, d: D) -> Union4<A, B, C, D> {
227+
pub fn d(self, d: D) -> Union4<A, B, C, D> {
215228
Union4 {
216229
raw: mask(D::erase(d), MASK_4, MASK_D),
217230
a: PhantomData,

0 commit comments

Comments
 (0)