-
Notifications
You must be signed in to change notification settings - Fork 12
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
d27a2b0
commit e7f42fe
Showing
2 changed files
with
116 additions
and
57 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,93 @@ | ||
# Cwerg Casting and Conversion | ||
# Cwerg Casting and Implicit Conversion | ||
|
||
## No cast required (implicit conversion) | ||
## Overview | ||
|
||
mut ptr T -> ptr T | ||
mut slice T -> slice T | ||
| Notation | Description | | ||
| --------------------- | -------------------------------------------------------------------- | | ||
| wrap_as(E, T) -> E | convert value to enum or wrapped type | | ||
| unwrap(E) -> E | convert enum or wrapped type to underlying type | | ||
| narrow_as(E, T) -> E | convert union value to actual type | | ||
| widen_as(E, T) -> E | convert value to union | | ||
| as(E, T) -> E | converts between numerical types | | ||
| bitwise_as(E, T) -> E | convert expression to a type of same width, including int to pointer | | ||
| unsafe_as(E, T) -> E | convert between pointers | | ||
|
||
sum A|B|C -> sum A|B|C|D|E | ||
A -> sum A|B|C|D|E | ||
## wrap_as / unwrap | ||
|
||
mut array T size -> mut slice T | ||
mut array T size -> slice T | ||
array T size -> slice T | ||
`unwrap` converts a value of a wrapped type or enum type to the underlying type. | ||
|
||
For array constants `array T size -> slice T` | ||
will materialized the array constant in | ||
readonly memory. | ||
``` | ||
enum color u8: | ||
red 1 | ||
green 2 | ||
blue 3 | ||
static_assert unwrap(color:green) == 2_u8 | ||
## Regular cast | ||
``` | ||
|
||
u8 -> u16, u32, u64, s16, s32, s64 | ||
u16 -> u32, u64, s32, s64 | ||
u32 -> u64, s64 | ||
`wrap_as` is the inverse operation: | ||
|
||
s8 -> s16, s32, s64 | ||
s16 -> s32, s64 | ||
s32 -> s64 | ||
``` | ||
static_assert wrap(2, color) == color:green | ||
``` | ||
|
||
r32 -> r64 | ||
Another example using wrapped type | ||
``` | ||
@wrapped type temperature_celsius = u16 | ||
## Bitcasts | ||
global freezing_point auto = wrap_as(100, temperature_celsius) | ||
``` | ||
|
||
s32 <-> u32 <-> r32 | ||
s64 <-> u64 <-> r64 | ||
## narrowing / widening | ||
|
||
sint <-> uint <-> ptr T | ||
TBD | ||
|
||
## Unsafe cast | ||
## Regular/numerical cast (`as`) | ||
|
||
ptr T -> mut ptr T | ||
slice T -> mut slice T | ||
Conversation between these numerical types attempting | ||
to preserve the value as much as possible | ||
|
||
TBD | ||
* u8, u16, u32, u64 | ||
* s8, s16, s32, s64 | ||
* r32, r64 | ||
|
||
## Bitcasts (`bitwise_as`) | ||
|
||
* s32/u32 <-> r32 | ||
* s64/u64 <-> r64 | ||
* sint/uint <-> ptr T | ||
|
||
## Unsafe cast (`unsafe_as`) | ||
|
||
* ptr A -> ptr B | ||
* ptr T -> mut ptr T | ||
* slice T -> mut slice T | ||
|
||
|
||
|
||
## No cast required (implicit conversion) | ||
|
||
### drop mutability | ||
|
||
mut ptr T -> ptr T | ||
mut slice T -> slice T | ||
|
||
### convert to a wider union | ||
|
||
|
||
sum A|B|C -> sum A|B|C|D|E | ||
A -> sum A|B|C|D|E | ||
|
||
This can be made explcit with the `widen_as` operation | ||
but is rare needed. | ||
|
||
### array to slice conversion | ||
|
||
mut array T size -> mut slice T | ||
mut array T size -> slice T | ||
array T size -> slice T | ||
|
||
For array constants `array T size -> slice T` | ||
will materialized the array constant in | ||
readonly memory. |
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