Skip to content

Commit

Permalink
implement type-conversion concept (#328)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xNeshi authored Dec 29, 2024
1 parent 988231e commit 42cdcfa
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 5 deletions.
6 changes: 2 additions & 4 deletions concepts/type-conversion/.meta/config.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
{
"blurb": "<todo>",
"authors": [
"<your_gh_username>"
],
"blurb": "Type conversion in Cairo allows you to transform values between different types using the `Into` and `TryInto` traits.",
"authors": ["0xNeshi"],
"contributors": []
}
73 changes: 73 additions & 0 deletions concepts/type-conversion/about.md
Original file line number Diff line number Diff line change
@@ -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<T>`.

## 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<T>`.

```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<Square, Rectangle> {
fn into(self: Square) -> Rectangle {
Rectangle { width: self.side_length, height: self.side_length }
}
}
```

### Example: `TryInto`

```rust
impl RectangleIntoSquare of TryInto<Rectangle, Square> {
fn try_into(self: Rectangle) -> Option<Square> {
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.
4 changes: 4 additions & 0 deletions concepts/type-conversion/introduction.md
Original file line number Diff line number Diff line change
@@ -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.
11 changes: 10 additions & 1 deletion concepts/type-conversion/links.json
Original file line number Diff line number Diff line change
@@ -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"
}
]

0 comments on commit 42cdcfa

Please sign in to comment.