-
Notifications
You must be signed in to change notification settings - Fork 584
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4f33533
commit 9592f1d
Showing
7 changed files
with
196 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/// A trait for dereferencing a value. This is used in order to directly access members of the | ||
/// dereferenced value. | ||
pub trait Deref<T> { | ||
type Target; | ||
fn deref(self: T) -> Self::Target; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -99,3 +99,4 @@ use core::{zeroable, zeroable::{NonZero, Zeroable}}; | |
|
||
#[cfg(test)] | ||
use core::test; | ||
pub use core::ops::Deref; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#[derive(Drop, Copy)] | ||
struct S1 { | ||
a: usize, | ||
b: felt252 | ||
} | ||
|
||
#[derive(Drop, Copy)] | ||
struct S2 { | ||
inner: S1, | ||
a: usize, | ||
} | ||
|
||
#[derive(Drop, Copy)] | ||
struct S3 { | ||
inner: S2 | ||
} | ||
|
||
|
||
impl S2Deref of core::ops::deref::Deref<S2> { | ||
type Target = S1; | ||
fn deref(self: S2) -> S1 { | ||
self.inner | ||
} | ||
} | ||
|
||
impl S3Deref of core::ops::deref::Deref<S3> { | ||
type Target = S2; | ||
fn deref(self: S3) -> S2 { | ||
self.inner | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_simple_deref() { | ||
let s1 = S1 { a: 1, b: 2 }; | ||
let s2 = S2 { inner: s1, a: 3 }; | ||
let s3 = S3 { inner: s2 }; | ||
assert_eq!(s1.a, 1); | ||
assert_eq!(s2.a, 3); | ||
assert_eq!(s3.a, 3); | ||
assert_eq!(s3.inner.a, 3); | ||
assert_eq!(s3.inner.inner.a, 1); | ||
assert_eq!(s3.b, 2); | ||
assert_eq!(s3.inner.b, 2); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters