Structs and Enums¶
FastC supports user-defined types through structs and enums.
Structs¶
Structs group related data together.
Declaration¶
Creating Instances¶
Use struct literals:
let p: Point = Point { x: 10, y: 20 };
let rect: Rectangle = Rectangle {
origin: Point { x: 0, y: 0 },
width: 100,
height: 50,
};
Field Access¶
Modifying Fields¶
Passing Structs¶
By value (copied):
By reference (efficient for large structs):
fn move_point(p: mref(Point), dx: i32, dy: i32) {
deref(p).x = deref(p).x + dx;
deref(p).y = deref(p).y + dy;
}
C-Compatible Layout¶
Use @repr(C) for guaranteed C-compatible memory layout:
This is required when:
- Passing structs to C functions
- Reading structs from files or network
- Interfacing with hardware
Enums¶
Enums define a type with a fixed set of variants.
Simple Enums¶
Using Enums¶
Switch on Enums¶
fn color_code(c: Color) -> i32 {
switch c {
case Color::Red:
return 0xFF0000;
case Color::Green:
return 0x00FF00;
case Color::Blue:
return 0x0000FF;
default:
return 0;
}
}
Enums with Data¶
Enums can carry associated data:
Examples¶
State Machine¶
enum ConnectionState {
Disconnected,
Connecting,
Connected,
Error,
}
fn next_state(current: ConnectionState, event: i32) -> ConnectionState {
switch current {
case ConnectionState::Disconnected:
if event == 1 {
return ConnectionState::Connecting;
}
return current;
case ConnectionState::Connecting:
if event == 2 {
return ConnectionState::Connected;
}
if event == 3 {
return ConnectionState::Error;
}
return current;
case ConnectionState::Connected:
if event == 4 {
return ConnectionState::Disconnected;
}
return current;
default:
return current;
}
}
Nested Structs¶
struct Address {
street: slice(u8),
city: slice(u8),
zip: i32,
}
struct Person {
name: slice(u8),
age: i32,
address: Address,
}
fn create_person() -> Person {
return Person {
name: c"Alice",
age: 30,
address: Address {
street: c"123 Main St",
city: c"Springfield",
zip: 12345,
},
};
}
Bit Flags¶
struct Permissions {
read: bool,
write: bool,
execute: bool,
}
fn can_access(perms: Permissions, need_write: bool) -> bool {
if need_write {
return perms.write;
}
return perms.read;
}
Generated C Code¶
A FastC struct:
Compiles to:
A FastC enum:
Compiles to:
Best Practices¶
- Use structs for related data - Group fields that belong together
- Use enums for fixed choices - State machines, options, error codes
- Use
@repr(C)for FFI - When interfacing with C code - Keep structs small - Large structs are expensive to copy
- Pass large structs by reference - Use
ref(T)ormref(T)