Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Precise bounds #19

Merged
merged 18 commits into from
Jun 1, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Copy, Clone: Demonstrate precise bounds in tests
ijackson committed Feb 20, 2024
commit 1daf89364b4ed2f01d1e15f68c74466883f74127
71 changes: 71 additions & 0 deletions tests/copy_clone_struct.rs
Original file line number Diff line number Diff line change
@@ -2,6 +2,8 @@
#![no_std]
#![allow(clippy::clone_on_copy)]

use core::marker::PhantomData;

use educe::Educe;

#[test]
@@ -109,3 +111,72 @@ fn bound_3() {
assert_eq!(1, s.f1);
assert_eq!(1, t.0);
}

#[test]
fn bound_4() {
#[derive(Educe)]
#[educe(Copy, Clone)]
struct Struct<T, U> {
f1: Option<T>,
f2: PhantomData<U>,
}

#[derive(Educe)]
#[educe(Copy, Clone)]
struct Tuple<T, U>(Option<T>, PhantomData<U>);

let s = Struct {
f1: Some(1), f2: PhantomData::<core::fmt::Formatter>
}
.clone();
let t = Tuple(Some(1), PhantomData::<core::fmt::Formatter>).clone();

assert_eq!(Some(1), s.f1);
assert_eq!(Some(1), t.0);
}

#[test]
fn bound_5() {
trait Suitable {}
struct SuitableNotClone;
impl Suitable for SuitableNotClone {}
let phantom = PhantomData::<SuitableNotClone>;

fn copy<T: Copy>(t: &T) -> T {
*t
}

#[derive(Educe)]
#[educe(Copy)]
struct Struct<T, U> {
f1: Option<T>,
f2: PhantomData<U>,
}

impl<T: Clone, U: Suitable> Clone for Struct<T, U> {
fn clone(&self) -> Self {
Struct {
f1: self.f1.clone(), f2: PhantomData
}
}
}

#[derive(Educe)]
#[educe(Copy)]
struct Tuple<T, U>(Option<T>, PhantomData<U>);

impl<T: Clone, U: Suitable> Clone for Tuple<T, U> {
fn clone(&self) -> Self {
Tuple(self.0.clone(), PhantomData)
}
}

let s = copy(&Struct {
f1: Some(1), f2: phantom
});

let t = copy(&Tuple(Some(1), phantom));

assert_eq!(Some(1), s.f1);
assert_eq!(Some(1), t.0);
}