From 3488225e099824a136d917220dd71ad8e0c66deb Mon Sep 17 00:00:00 2001 From: Nenad Date: Sun, 29 Dec 2024 10:36:30 +0100 Subject: [PATCH] implement type-conversion concept --- concepts/type-conversion/.meta/config.json | 6 +- concepts/type-conversion/about.md | 73 ++++++++++++++++++++++ concepts/type-conversion/introduction.md | 4 ++ concepts/type-conversion/links.json | 11 +++- 4 files changed, 89 insertions(+), 5 deletions(-) diff --git a/concepts/type-conversion/.meta/config.json b/concepts/type-conversion/.meta/config.json index 35b1d32b..2a471bcc 100644 --- a/concepts/type-conversion/.meta/config.json +++ b/concepts/type-conversion/.meta/config.json @@ -1,7 +1,5 @@ { - "blurb": "", - "authors": [ - "" - ], + "blurb": "Type conversion in Cairo allows you to transform values between different types using the `Into` and `TryInto` traits.", + "authors": ["0xNeshi"], "contributors": [] } diff --git a/concepts/type-conversion/about.md b/concepts/type-conversion/about.md index 7b930e51..58f70b64 100644 --- a/concepts/type-conversion/about.md +++ b/concepts/type-conversion/about.md @@ -1 +1,74 @@ # Type Conversion + +Cairo supports type conversion through the `Into` and `TryInto` traits from the core library, commonly used for in-built and custom types. + +These traits define methods for converting values between types: + +- **`Into`**: Used for guaranteed, infallible conversions. +- **`TryInto`**: Used for fallible conversions, returning `Option`. + +## Using `Into` + +`Into` handles conversions where the target type can always accommodate the source value. + +```rust +fn main() { + let my_u8: u8 = 10; + let my_felt252: felt252 = my_u8.into(); // Infallible conversion + let my_u256: u256 = my_felt252.into(); // Conversion to larger types +} +``` + +## Using `TryInto` + +`TryInto` is for cases where the conversion might fail, returning `Option`. + +```rust +fn main() { + let my_u256: u256 = 10; + let my_felt252: felt252 = my_u256.try_into().unwrap(); // Success + let my_large_u16: u16 = 2048; + let my_large_u8: u8 = my_large_u16.try_into().unwrap(); // Panics because value doesn't fit in `u8` +} +``` + +## Custom Type Conversion + +You can implement these traits for your own types. + +### Example: `Into` + +```rust +#[derive(Drop)] +struct Rectangle { + width: u64, + height: u64, +} + +#[derive(Drop)] +struct Square { + side_length: u64, +} + +impl SquareIntoRectangle of Into { + fn into(self: Square) -> Rectangle { + Rectangle { width: self.side_length, height: self.side_length } + } +} +``` + +### Example: `TryInto` + +```rust +impl RectangleIntoSquare of TryInto { + fn try_into(self: Rectangle) -> Option { + if self.height == self.width { + Option::Some(Square { side_length: self.height }) + } else { + Option::None + } + } +} +``` + +This setup provides flexible and type-safe conversions for both built-in and custom types in Cairo. diff --git a/concepts/type-conversion/introduction.md b/concepts/type-conversion/introduction.md index e10b99d0..150e8ed5 100644 --- a/concepts/type-conversion/introduction.md +++ b/concepts/type-conversion/introduction.md @@ -1 +1,5 @@ # Introduction + +Type conversion in Cairo allows you to transform values between different types using the `Into` and `TryInto` traits. +The `Into` trait is used for guaranteed conversions, while `TryInto` handles fallible conversions that may fail if the value doesn't fit the target type. +These traits can also be implemented for custom types, enabling flexible and precise conversions in your programs. diff --git a/concepts/type-conversion/links.json b/concepts/type-conversion/links.json index fe51488c..919f905c 100644 --- a/concepts/type-conversion/links.json +++ b/concepts/type-conversion/links.json @@ -1 +1,10 @@ -[] +[ + { + "url": "https://book.cairo-lang.org/ch02-02-data-types.html#type-conversion", + "description": "Type Conversion in The Cairo Book" + }, + { + "url": "https://book.cairo-lang.org/ch05-02-an-example-program-using-structs.html#conversions-of-custom-types", + "description": "Conversions of Custom Types in The Cairo Book" + } +]