Refactored componenst
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
---
|
||||
title: Classes
|
||||
description: Unified data and behavior with Java-style organization and Rust-powered execution.
|
||||
icon: Shapes
|
||||
---
|
||||
|
||||
Classes in Mist bridge the gap between Java's organizational structure and Rust's performance. They allow you to define data fields, constructors, instance methods, and trait implementations within a single, cohesive block.
|
||||
|
||||
## Basic Syntax
|
||||
|
||||
A class groups fields and methods together. Fields follow the `type name` convention, and methods define their logic directly within the class body.
|
||||
|
||||
```cpp
|
||||
public class Logger {
|
||||
String prefix;
|
||||
|
||||
public void info(self*, str* message) {
|
||||
self.log(LogLevel::Info, message);
|
||||
}
|
||||
|
||||
void log(self*, LogLevel level, str* message) {
|
||||
println!("{level} {} {}", self.prefix, message);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## The Constructor
|
||||
|
||||
Unlike languages that use the class name for initialization, Mist uses the explicit `constructor` keyword. This makes the entry point of the class unmistakable.
|
||||
|
||||
```cpp
|
||||
public constructor(str* prefix) {
|
||||
self.prefix = prefix.to_string();
|
||||
}
|
||||
```
|
||||
|
||||
## Instance Methods & `self`
|
||||
|
||||
Mist maintains Rust's explicit context handling. Any method that needs to access or modify class data must include `self*` (or `self mut*` for mutations) as its first parameter.
|
||||
|
||||
```cpp
|
||||
public void warning(self*, str* message) {
|
||||
self.log(LogLevel::Warning, message);
|
||||
}
|
||||
```
|
||||
|
||||
## Trait Implementations
|
||||
|
||||
One of Mist's most powerful features is the ability to nest trait implementations directly within the class block. This keeps the logic for how a type behaves (e.g., how it is displayed) physically coupled with the type definition.
|
||||
|
||||
```cpp
|
||||
impl fmt::Display {
|
||||
std::fmt::Result fmt(self*, std::fmt::Formatter<'_> mut* f) {
|
||||
return write!(f, "logger ({})", self.prefix);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Key Characteristics
|
||||
|
||||
- **Unified Scope**: Data, behavior, and trait logic live in one place, eliminating the friction of jumping between `struct` and `impl` blocks.
|
||||
- **Explicit Context**: The use of `self*` ensures that the relationship between a method and its instance is always transparent.
|
||||
- **Encapsulation**: Visibility modifiers (`public`) allow you to expose a clean API while keeping internal helper methods and state private to the class.
|
||||
- **Zero-Cost Classes**: Under the hood, Mist desugars these into idiomatic Rust structs and implementation blocks, ensuring no runtime overhead compared to raw Rust.
|
||||
@@ -0,0 +1,57 @@
|
||||
---
|
||||
title: Enums
|
||||
description: Defining algebraic data types with Mist's type-first convention.
|
||||
icon: Layers
|
||||
---
|
||||
|
||||
Enums in Mist serve as powerful algebraic data types (ADTs), maintaining the exact behavior and safety of Rust enums while applying the language-wide `type name` convention for variants that contain data.
|
||||
|
||||
## Basic Syntax
|
||||
|
||||
An enum can contain unit variants, tuple variants, or struct-like variants. Following Mist's core philosophy, struct-like variants place the type before the identifier.
|
||||
|
||||
```rust
|
||||
public enum TaskState {
|
||||
Pending,
|
||||
InProgress,
|
||||
Completed,
|
||||
// Struct-like variant using 'type name'
|
||||
Failed {
|
||||
String reason,
|
||||
i32 code,
|
||||
},
|
||||
}
|
||||
```
|
||||
## Variant Types
|
||||
|
||||
Mist supports all standard variant shapes, ensuring a 1:1 mapping to the underlying Rust execution model.
|
||||
|
||||
```rust
|
||||
enum Message {
|
||||
Quit, // Unit
|
||||
Move(i32, i32), // Tuple
|
||||
Write(String), // Tuple
|
||||
ChangeColor { // Struct-like
|
||||
u8 r, u8 g, u8 b,
|
||||
},
|
||||
}
|
||||
```
|
||||
## Generics & Lifetimes
|
||||
|
||||
Just like structs and functions, enums declare generics and lifetimes in a unified block. This is particularly useful for defining custom Result or Option types that handle references.
|
||||
|
||||
```rust
|
||||
public enum Validation<'a, T> {
|
||||
Valid(T),
|
||||
Invalid {
|
||||
str'a* message,
|
||||
u32 error_id,
|
||||
},
|
||||
}
|
||||
```
|
||||
## Key Characteristics
|
||||
|
||||
- **Consistent Member Declaration**: Struct-like variants maintain the `type name` order, ensuring that data modeling feels identical whether you are defining a top-level `struct` or an `enum` variant.
|
||||
- **Rust-Native ADTs**: Enums compile directly to Rust enums, allowing for exhaustive pattern matching and zero-cost abstraction.
|
||||
- **Shared Visibility**: The `public` modifier at the enum level exports all variants for use in other modules, matching Rust's visibility rules for enums.
|
||||
- **Comma-Separated Members**: Fields within struct-like variants are separated by commas, mirroring the syntax used in standard Mist structs.
|
||||
@@ -0,0 +1,69 @@
|
||||
---
|
||||
title: Functions
|
||||
description: Defining execution blocks with C-style ergonomics and Rust-powered safety.
|
||||
icon: SquareFunction
|
||||
---
|
||||
|
||||
Functions are the primary unit of execution in Mist. They prioritize a traditional declaration order, placing the return type before the identifier to ensure signatures remain easy to scan in complex systems.
|
||||
|
||||
## Basic Syntax
|
||||
|
||||
A standard function requires a return type, a name, and a body. Use the `void` keyword for functions that do not return a value.
|
||||
|
||||
```cpp
|
||||
i32 add(i32 a, i32 b) {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
void log_status(str* message) {
|
||||
println!("{}", message);
|
||||
}
|
||||
```
|
||||
|
||||
## Visibility & Exports
|
||||
|
||||
Functions are private to their module by default. The `public` modifier exports the function for cross-module access.
|
||||
|
||||
```cpp
|
||||
public i32 get_version() {
|
||||
return 1;
|
||||
}
|
||||
```
|
||||
|
||||
## Mutable Parameters
|
||||
|
||||
Parameters follow Rust’s ownership rules but use Mist’s local variable syntax. Use `mut` to allow a function to modify its local binding of a value.
|
||||
|
||||
```cpp
|
||||
void update_score(i32 mut current_score, i32 bonus) {
|
||||
current_score = current_score + bonus;
|
||||
}
|
||||
```
|
||||
|
||||
## Generics & Lifetimes
|
||||
|
||||
Mist integrates type abstraction and memory management into a single generic block. Lifetimes and type parameters share the `< >` bracket following the identifier.
|
||||
|
||||
```rust
|
||||
public str'a* choose_longer<'a, T: Display>(str'a* s1, str'a* s2, T meta) {
|
||||
println!("Metadata: {}", meta);
|
||||
return if (s1.len() > s2.len()) { s1 } else { s2 };
|
||||
}
|
||||
```
|
||||
|
||||
## Attributes & Metadata
|
||||
|
||||
Metadata is applied via the `#[attr]` syntax directly above the declaration for compiler hints or testing.
|
||||
|
||||
```cpp
|
||||
#[inline]
|
||||
public bool is_active(u32 id) {
|
||||
return id > 0;
|
||||
}
|
||||
```
|
||||
|
||||
## Key Characteristics
|
||||
|
||||
* **Scannable Signatures:** Placing return types first allows for rapid identification of a function's output.
|
||||
* **Unified Abstraction:** Lifetimes and type constraints are declared in one location, reducing signature noise.
|
||||
* **Zero-Cost Mapping:** Every function maps directly to a Rust `fn`, maintaining performance and ecosystem compatibility.
|
||||
@@ -0,0 +1,39 @@
|
||||
---
|
||||
title: Pointers & References
|
||||
description: Explicit memory access with C-style ergonomics and Rust-native safety.
|
||||
icon: MousePointer2
|
||||
---
|
||||
|
||||
Mist simplifies Rust’s reference system by using a pointer-style syntax. While the symbols look like C-style pointers, they adhere strictly to Rust’s ownership and borrowing rules.
|
||||
|
||||
## Basic Syntax
|
||||
|
||||
References are defined by placing a `*` after the type. By default, pointers are immutable (shared). To allow modification of the underlying data, use the `mut*` modifier.
|
||||
|
||||
```cpp
|
||||
void increment(i32 mut* value, i32* limit) {
|
||||
if (value < limit) {
|
||||
value = value + 1;
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
## Lifetimes
|
||||
|
||||
Lifetimes are attached directly to the type before the pointer symbol. This maintains a clean visual flow where the "type-contract" (identity, duration, and mutability) is read from left to right.
|
||||
|
||||
```cpp
|
||||
public struct Inspector<'a> {
|
||||
public str'a* target,
|
||||
public u32'a mut* counter,
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
## Key Characteristics
|
||||
|
||||
- **Explicit Intent:** The `mut*` syntax clearly distinguishes between a reference that can read and one that can write, mapping 1:1 to Rust's `&` and `&mut`.
|
||||
- **Visual Consistency:** Lifetimes (`'a`) and mutability modifiers are integrated into the type declaration, keeping function signatures and struct fields compact.
|
||||
- **Safety Guaranteed:** Despite the "pointer" appearance, the Mist compiler enforces Rust’s borrow checker. You cannot have multiple `mut*` references to the same data, and references cannot outlive their owners.
|
||||
- **Zero Overhead:** Mist pointers are "thin" or "fat" exactly like Rust references; they carry no extra runtime metadata and compile to identical machine code.
|
||||
@@ -0,0 +1,60 @@
|
||||
---
|
||||
title: Structs
|
||||
description: Data modeling using Mist's type-first convention.
|
||||
icon: Form
|
||||
---
|
||||
|
||||
Structs in Mist follow the same structural logic as Rust, but apply the language-wide `type name` declaration style and allow for comma-separated field grouping.
|
||||
|
||||
## Basic Syntax
|
||||
|
||||
A struct is defined by its name followed by a block of fields. Each field follows the Mist convention of placing the type before the identifier, separated by commas.
|
||||
|
||||
```rust
|
||||
public struct Task {
|
||||
public String name,
|
||||
public TaskState state,
|
||||
public i32 executions,
|
||||
}
|
||||
```
|
||||
|
||||
## Visibility
|
||||
|
||||
Use the `public` modifier to make the struct or its individual fields accessible from other modules.
|
||||
|
||||
```cpp
|
||||
public struct NetworkNode {
|
||||
public u32 id,
|
||||
str* address,
|
||||
}
|
||||
```
|
||||
|
||||
## Instantiation
|
||||
|
||||
Structs are instantiated using the standard brace syntax.
|
||||
|
||||
```cpp
|
||||
var task = Task {
|
||||
name: "Initialize".to_string(),
|
||||
state: TaskState::Pending,
|
||||
executions: 0,
|
||||
};
|
||||
```
|
||||
|
||||
## Generics & Lifetimes
|
||||
|
||||
Generics and lifetimes are declared in angle brackets after the struct name. Lifetimes are associated with the reference/pointer type within the field declarations.
|
||||
|
||||
```cpp
|
||||
public struct Buffer<'a, T> {
|
||||
public T'a* data,
|
||||
public usize len,
|
||||
}
|
||||
```
|
||||
|
||||
## Key Characteristics
|
||||
|
||||
- **Type-First Declaration**: Fields use the `type name` order to match function parameters and variable declarations.
|
||||
- **Comma-Separated Members**: Fields are separated by commas, maintaining a clean and consistent delimiter style.
|
||||
- **Rust Compatibility**: Maps 1:1 to Rust structs, ensuring zero-cost abstraction and full ecosystem interoperability.
|
||||
- **Direct Visibility**: The `public` keyword replaces `pub` for a more consistent modifier language across the codebase.
|
||||
@@ -0,0 +1,61 @@
|
||||
---
|
||||
title: Traits
|
||||
description: Defining shared behavior and contracts with Mist's signature ergonomics.
|
||||
icon: Sparkles
|
||||
---
|
||||
|
||||
Traits in Mist define a set of methods that a type must implement, facilitating polymorphism and shared behavior. While they mirror the logic of Rust traits, they utilize Mist’s **type-first** declaration style for method signatures.
|
||||
|
||||
## Defining a Trait
|
||||
|
||||
A trait definition lists method signatures that implementing types must satisfy. Like functions, these signatures place the return type before the method name.
|
||||
|
||||
```rust
|
||||
public trait Drawable {
|
||||
void draw(self*);
|
||||
str* metadata(self*);
|
||||
}
|
||||
```
|
||||
## Implementing a Trait
|
||||
|
||||
To implement a trait for a specific type, use the `impl` keyword followed by the trait name and the target type. This block must contain all required methods defined in the trait.
|
||||
|
||||
```rust
|
||||
impl Drawable for Task {
|
||||
void draw(self*) {
|
||||
println!("Drawing task: {}", self.name);
|
||||
}
|
||||
|
||||
str* metadata(self*) {
|
||||
return self.name;
|
||||
}
|
||||
}
|
||||
```
|
||||
## Default Implementations
|
||||
|
||||
Traits can provide default behavior for methods. Types implementing the trait can choose to override these defaults or use the provided implementation.
|
||||
|
||||
```rust
|
||||
public trait Identifiable {
|
||||
u32 get_id(self*);
|
||||
|
||||
bool is_valid(self*) {
|
||||
return self.get_id() > 0;
|
||||
}
|
||||
}
|
||||
```
|
||||
## Super-traits
|
||||
|
||||
Traits can build upon other traits. If a trait requires another trait to be implemented first, use the colon `:` syntax.
|
||||
|
||||
```rust
|
||||
public trait Animated : Drawable {
|
||||
void animate(self*, f32 delta_time);
|
||||
}
|
||||
```
|
||||
## Key Characteristics
|
||||
|
||||
- **Consistent Signatures**: Method signatures within traits follow the language-wide `return_type name(params)` convention.
|
||||
- **Explicit Context**: Methods use `self*` or `self mut*` as the first parameter to define how the instance is accessed, mapping directly to Rust's reference rules.
|
||||
- **Static Dispatch**: By default, Mist traits leverage Rust's zero-cost generics and monomorphization, ensuring high performance.
|
||||
- **Predictable Contracts**: Traits act as strict blueprints; the Mist compiler ensures every implementation perfectly matches the interface before generating the corresponding Rust code.
|
||||
Reference in New Issue
Block a user