forked from cairo-book/cairo-book
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Advanced Cairo: Using arrays inside dicts (cairo-book#827)
- Loading branch information
Nenad Misić
authored
Jun 1, 2024
1 parent
bf27828
commit b06f032
Showing
26 changed files
with
166 additions
and
23 deletions.
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
listings/ch03-common-collections/no_listing_14_dict_of_array_insert/.gitignore
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 @@ | ||
target |
9 changes: 9 additions & 0 deletions
9
listings/ch03-common-collections/no_listing_14_dict_of_array_insert/Scarb.toml
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,9 @@ | ||
[package] | ||
name = "no_listing_14_dict_of_array_insert" | ||
version = "0.1.0" | ||
edition = "2023_11" | ||
|
||
# See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest | ||
|
||
[dependencies] | ||
# foo = { path = "vendor/foo" } |
6 changes: 6 additions & 0 deletions
6
listings/ch03-common-collections/no_listing_14_dict_of_array_insert/src/lib.cairo
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 @@ | ||
fn main() { | ||
let arr = array![20, 19, 26]; | ||
let mut dict: Felt252Dict<Nullable<Array<u8>>> = Default::default(); | ||
dict.insert(0, NullableTrait::new(arr)); | ||
println!("Array inserted successfully."); | ||
} |
1 change: 1 addition & 0 deletions
1
listings/ch03-common-collections/no_listing_15_dict_of_array_attempt_get/.gitignore
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 @@ | ||
target |
9 changes: 9 additions & 0 deletions
9
listings/ch03-common-collections/no_listing_15_dict_of_array_attempt_get/Scarb.toml
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,9 @@ | ||
[package] | ||
name = "no_listing_15_dict_of_array_attempt_get" | ||
version = "0.1.0" | ||
edition = "2023_11" | ||
|
||
# See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest | ||
|
||
[dependencies] | ||
# foo = { path = "vendor/foo" } |
9 changes: 9 additions & 0 deletions
9
listings/ch03-common-collections/no_listing_15_dict_of_array_attempt_get/output.txt
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,9 @@ | ||
$ scarb cairo-run | ||
Compiling no_listing_15_dict_of_array_attempt_get v0.1.0 (listings/ch03-common-collections/no_listing_15_dict_of_array_attempt_get/Scarb.toml) | ||
error: Trait has no implementation in context: core::traits::Copy::<core::nullable::Nullable::<core::array::Array::<core::integer::u8>>> | ||
--> listings/ch03-common-collections/no_listing_15_dict_of_array_attempt_get/src/lib.cairo:11:20 | ||
let arr = pixels.get(0); | ||
^*^ | ||
|
||
error: could not compile `no_listing_15_dict_of_array_attempt_get` due to previous error | ||
error: `scarb metadata` exited with error |
18 changes: 18 additions & 0 deletions
18
listings/ch03-common-collections/no_listing_15_dict_of_array_attempt_get/src/lib.cairo
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,18 @@ | ||
//TAG: does_not_compile | ||
use core::nullable::{match_nullable, FromNullableResult}; | ||
|
||
fn main() { | ||
let arr = array![20, 19, 26]; | ||
let mut dict: Felt252Dict<Nullable<Array<u8>>> = Default::default(); | ||
dict.insert(0, NullableTrait::new(arr)); | ||
println!("Array: {:?}", get_array_entry(ref dict, 0)); | ||
} | ||
|
||
fn get_array_entry(ref dict: Felt252Dict<Nullable<Array<u8>>>, index: felt252) -> Span<u8> { | ||
let val = dict.get(0); // This will cause a compiler error | ||
let arr = match match_nullable(val) { | ||
FromNullableResult::Null => panic!("No value!"), | ||
FromNullableResult::NotNull(val) => val.unbox() | ||
}; | ||
arr.span() | ||
} |
1 change: 1 addition & 0 deletions
1
listings/ch03-common-collections/no_listing_16_dict_of_array/.gitignore
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 @@ | ||
target |
9 changes: 9 additions & 0 deletions
9
listings/ch03-common-collections/no_listing_16_dict_of_array/Scarb.toml
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,9 @@ | ||
[package] | ||
name = "no_listing_16_dict_of_array" | ||
version = "0.1.0" | ||
edition = "2023_11" | ||
|
||
# See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest | ||
|
||
[dependencies] | ||
# foo = { path = "vendor/foo" } |
36 changes: 36 additions & 0 deletions
36
listings/ch03-common-collections/no_listing_16_dict_of_array/src/lib.cairo
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,36 @@ | ||
//ANCHOR: all | ||
use core::nullable::NullableTrait; | ||
use core::dict::Felt252DictEntryTrait; | ||
|
||
//ANCHOR: append | ||
fn append_value(ref dict: Felt252Dict<Nullable<Array<u8>>>, index: felt252, value: u8) { | ||
let (entry, arr) = dict.entry(index); | ||
let mut unboxed_val = arr.deref_or(array![]); | ||
unboxed_val.append(value); | ||
dict = entry.finalize(NullableTrait::new(unboxed_val)); | ||
} | ||
//ANCHOR_END: append | ||
|
||
//ANCHOR: get | ||
fn get_array_entry(ref dict: Felt252Dict<Nullable<Array<u8>>>, index: felt252) -> Span<u8> { | ||
let (entry, _arr) = dict.entry(index); | ||
let mut arr = _arr.deref_or(array![]); | ||
let span = arr.span(); | ||
dict = entry.finalize(NullableTrait::new(arr)); | ||
span | ||
} | ||
//ANCHOR_END: get | ||
|
||
fn main() { | ||
let arr = array![20, 19, 26]; | ||
let mut dict: Felt252Dict<Nullable<Array<u8>>> = Default::default(); | ||
dict.insert(0, NullableTrait::new(arr)); | ||
println!("Before insertion: {:?}", get_array_entry(ref dict, 0)); | ||
|
||
append_value(ref dict, 0, 30); | ||
|
||
println!("After insertion: {:?}", get_array_entry(ref dict, 0)); | ||
} | ||
//ANCHOR_END: all | ||
|
||
|
File renamed without changes.
File renamed without changes.
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
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
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 was deleted.
Oops, something went wrong.
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
File renamed without changes.
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
File renamed without changes.
File renamed without changes.
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