Skip to content

Commit acd03a1

Browse files
authored
feat: anonymous generic impl parameters (#469)
1 parent 63f9149 commit acd03a1

File tree

55 files changed

+125
-99
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+125
-99
lines changed

book.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ src = "src"
66
title = "The Cairo Programming Language"
77

88
[build]
9-
extra-watch-dirs = ["po"]
9+
extra-watch-dirs = ["po", "listings"]
1010

1111
[preprocessor.cairo]
1212
after = ["links"]

listings/ch03-common-collections/no_listing_10_custom_methods/src/lib.cairo

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use dict::Felt252DictEntryTrait;
55
use debug::PrintTrait;
66

77
// ANCHOR: custom_get
8-
fn custom_get<T, impl TDefault: Felt252DictValue<T>, impl TDrop: Drop<T>, impl TCopy: Copy<T>>(
8+
fn custom_get<T, +Felt252DictValue<T>, +Drop<T>, +Copy<T>>(
99
ref dict: Felt252Dict<T>, key: felt252
1010
) -> T {
1111
// Get the new entry and the previous value held at `key`
@@ -23,13 +23,7 @@ fn custom_get<T, impl TDefault: Felt252DictValue<T>, impl TDrop: Drop<T>, impl T
2323
// ANCHOR_END: custom_get
2424

2525
// ANCHOR: custom_insert
26-
fn custom_insert<
27-
T,
28-
impl TDefault: Felt252DictValue<T>,
29-
impl TDestruct: Destruct<T>,
30-
impl TPrint: PrintTrait<T>,
31-
impl TDrop: Drop<T>
32-
>(
26+
fn custom_insert<T, +Felt252DictValue<T>, +Destruct<T>, +PrintTrait<T>, +Drop<T>>(
3327
ref dict: Felt252Dict<T>, key: felt252, value: T
3428
) {
3529
// Get the last entry associated with `key`

listings/ch03-common-collections/no_listing_12_dict_struct_member/src/lib.cairo

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,33 @@ struct UserDatabase<T> {
88
// ANCHOR: trait
99
trait UserDatabaseTrait<T> {
1010
fn new() -> UserDatabase<T>;
11-
fn add_user<impl TDrop: Drop<T>>(ref self: UserDatabase<T>, name: felt252, balance: T);
12-
fn get_balance<impl TCopy: Copy<T>>(ref self: UserDatabase<T>, name: felt252) -> T;
11+
fn add_user<+Drop<T>>(ref self: UserDatabase<T>, name: felt252, balance: T);
12+
fn get_balance<+Copy<T>>(ref self: UserDatabase<T>, name: felt252) -> T;
1313
}
1414
// ANCHOR_END: trait
1515

1616
// ANCHOR: impl
17-
impl UserDatabaseImpl<T, impl TDefault: Felt252DictValue<T>> of UserDatabaseTrait<T> {
17+
impl UserDatabaseImpl<T, +Felt252DictValue<T>> of UserDatabaseTrait<T> {
1818
// Creates a database
1919
fn new() -> UserDatabase<T> {
2020
UserDatabase { users_amount: 0, balances: Default::default() }
2121
}
2222

2323
// Get the user's balance
24-
fn get_balance<impl TCopy: Copy<T>>(ref self: UserDatabase<T>, name: felt252) -> T {
24+
fn get_balance<+Copy<T>>(ref self: UserDatabase<T>, name: felt252) -> T {
2525
self.balances.get(name)
2626
}
2727

2828
// Add a user
29-
fn add_user<impl TDrop: Drop<T>>(ref self: UserDatabase<T>, name: felt252, balance: T) {
29+
fn add_user<+Drop<T>>(ref self: UserDatabase<T>, name: felt252, balance: T) {
3030
self.balances.insert(name, balance);
3131
self.users_amount += 1;
3232
}
3333
}
3434
// ANCHOR_END: impl
3535

3636
// ANCHOR: destruct
37-
impl UserDatabaseDestruct<
38-
T, impl TDrop: Drop<T>, impl TDefault: Felt252DictValue<T>
39-
> of Destruct<UserDatabase<T>> {
37+
impl UserDatabaseDestruct<T, +Drop<T>, +Felt252DictValue<T>> of Destruct<UserDatabase<T>> {
4038
fn destruct(self: UserDatabase<T>) nopanic {
4139
self.balances.squash();
4240
}

listings/ch03-common-collections/no_listing_13_cust_struct_vect/src/lib.cairo

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ struct NullableVec<T> {
2323
}
2424
// ANCHOR_END: struct
2525

26-
impl DestructNullableVec<T, impl TDrop: Drop<T>> of Destruct<NullableVec<T>> {
26+
impl DestructNullableVec<T, +Drop<T>> of Destruct<NullableVec<T>> {
2727
fn destruct(self: NullableVec<T>) nopanic {
2828
self.data.squash();
2929
}
3030
}
3131

3232
// ANCHOR: implem
33-
impl NullableVecImpl<T, impl TDrop: Drop<T>, impl TCopy: Copy<T>> of VecTrait<NullableVec<T>, T> {
33+
impl NullableVecImpl<T, +Drop<T>, +Copy<T>> of VecTrait<NullableVec<T>, T> {
3434
fn new() -> NullableVec<T> {
3535
NullableVec { data: Default::default(), len: 0 }
3636
}

listings/ch03-common-collections/no_listing_14_cust_struct_stack/src/lib.cairo

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,15 @@ struct NullableStack<T> {
2020
}
2121
// ANCHOR_END: struct
2222

23-
impl DestructNullableStack<T, impl TDrop: Drop<T>> of Destruct<NullableStack<T>> {
23+
impl DestructNullableStack<T, +Drop<T>> of Destruct<NullableStack<T>> {
2424
fn destruct(self: NullableStack<T>) nopanic {
2525
self.data.squash();
2626
}
2727
}
2828

2929

3030
// ANCHOR: implem
31-
impl NullableStackImpl<
32-
T, impl TDrop: Drop<T>, impl TCopy: Copy<T>
33-
> of StackTrait<NullableStack<T>, T> {
31+
impl NullableStackImpl<T, +Drop<T>, +Copy<T>> of StackTrait<NullableStack<T>, T> {
3432
fn push(ref self: NullableStack<T>, value: T) {
3533
self.data.insert(self.len.into(), nullable_from_box(BoxTrait::new(value)));
3634
self.len += 1;

listings/ch08-generic-types-and-traits/no_listing_02_with_tdrop/src/lib.cairo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
fn largest_list<T, impl TDrop: Drop<T>>(l1: Array<T>, l2: Array<T>) -> Array<T> {
1+
fn largest_list<T, +Drop<T>>(l1: Array<T>, l2: Array<T>) -> Array<T> {
22
if l1.len() > l2.len() {
33
l1
44
} else {

listings/ch08-generic-types-and-traits/no_listing_03_missing_tcopy/src/lib.cairo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
// Given a list of T get the smallest one.
44
// The PartialOrd trait implements comparison operations for T
5-
fn smallest_element<T, impl TPartialOrd: PartialOrd<T>>(list: @Array<T>) -> T {
5+
fn smallest_element<T, +PartialOrd<T>>(list: @Array<T>) -> T {
66
// This represents the smallest element through the iteration
77
// Notice that we use the desnap (*) operator
88
let mut smallest = *list[0];
File renamed without changes.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "no_listing_05_with_anonymous_impl"
3+
version = "0.1.0"
4+
5+
# See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest
6+
7+
[dependencies]
8+
# foo = { path = "vendor/foo" }
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
fn smallest_element<T, +PartialOrd<T>, +Copy<T>, +Drop<T>>(list: @Array<T>) -> T {
2+
let mut smallest = *list[0];
3+
let mut index = 1;
4+
loop {
5+
if index >= list.len() {
6+
break smallest;
7+
}
8+
if *list[index] < smallest {
9+
smallest = *list[index];
10+
}
11+
index = index + 1;
12+
}
13+
}

0 commit comments

Comments
 (0)