Skip to content

Commit

Permalink
doc: Update
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmah309 committed Jul 2, 2024
1 parent 368eb35 commit a99d85f
Show file tree
Hide file tree
Showing 19 changed files with 64 additions and 87 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
# rust_core

***
[![Pub Version](https://img.shields.io/pub/v/rust_core.svg)](https://pub.dev/packages/rust_core)
[![Dart Package Docs](https://img.shields.io/badge/documentation-pub.dev-blue.svg)](https://pub.dev/documentation/rust_core/latest/)
[![License: MIT](https://img.shields.io/badge/license-MIT-purple.svg)](https://opensource.org/licenses/MIT)
[![License: Apache 2.0](https://img.shields.io/github/license/mcmah309/path_type)](https://opensource.org/license/apache-2-0)
[![Build Status](https://github.com/mcmah309/rust_core/actions/workflows/test.yml/badge.svg)](https://github.com/mcmah309/rust_core/actions)

[rust_core](https://github.com/mcmah309/rust_core) is an implementation of Rust's Core Library in Dart, bringing the power of Rust to Dart!

[Rust Core Book 📖](https://mcmah309.github.io/rust_core)

## Example
***
> Goal: Get the index of every "!" in a string not followed by a "?"
**Rust:**
```rust
use std::iter::Peekable;

fn main() {
let mut answer: Vec<usize> = Vec::new();
let string = "kl!sd!?!";
let mut answer: Vec<usize> = Vec::new();
let mut iter: Peekable<_> = string
.chars()
.map_windows(|w: &[char; 2]| *w)
Expand All @@ -41,9 +42,9 @@ fn main() {
import 'package:rust_core/rust_core.dart';
void main() {
List<int> answer = [];
String string = "kl!sd!?!";
Peekable<(int index, Arr<String> window)> iter = string
List<int> answer = [];
Peekable<(int, Arr<String>)> iter = string
.chars()
.mapWindows(2, identity)
.enumerate()
Expand All @@ -64,7 +65,7 @@ void main() {
```

# Project Goals

***
rust_core's primary goal is to bring Rust's features and ergonomics to Dart.

To accomplish this, Rust's functionalities are carefully adapted to Dart's paradigms, focusing on a smooth idiomatic language-compatible integration.
Expand Down
2 changes: 1 addition & 1 deletion book/src/introduction/new_to_dart.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# New To Dart

***
Welcome to Dart!

Dart is a great language choice for fast cross platform development and scripting.
Expand Down
2 changes: 1 addition & 1 deletion book/src/introduction/new_to_rust.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# New To Rust

***
Welcome to Rust!

Maybe you have heard of Rust and want to see what all the hype is about, maybe you know a little Rust
Expand Down
7 changes: 4 additions & 3 deletions book/src/introduction/quickstart.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Quickstart

## Setup
***
### Install

rust_core can be install like any other Dart package.
Expand Down Expand Up @@ -33,13 +34,13 @@ or in it's entirety
import 'package:rust_core/rust_core.dart';
```
## General Notes

***
All of rust_core's classes and methods are well documented in the [docs](https://pub.dev/documentation/rust_core/latest/), but
being an implementation of Rust's core library, you can also refer to [Rust core](https://doc.rust-lang.org/beta/core/index.html) if anything is unclear.
The functionally is the same.

## The Basics

***
### Result and Option

`Result<T, E>` is a sum type used for returning and propagating errors - `Ok` and `Err`.
Expand Down Expand Up @@ -118,5 +119,5 @@ expect(list, [1, 2, 10, 4, 5]);
`Slice` also has <u>a lot</u> of efficient methods for in-place mutation within and between slices - [docs](https://pub.dev/documentation/rust_core/latest/slice/slice-library.html)

## Whats Next?

***
Checkout any of the other sections in the book for more details and enjoy!
2 changes: 1 addition & 1 deletion book/src/libs/array/array.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Array

***
`Arr` (Array) is a zero cost extension type of `List`, where the `List` is treated as non-growable. This is useful for correctly handling lists where growable is false and const lists - as these types of lists are treated the same in the regular Dart type system, which may lead to errors. With `Arr`, type intent is clear for maintainers and developers are able think about code performance more critically.
```dart
var array = Arr(null, 10);
Expand Down
2 changes: 1 addition & 1 deletion book/src/libs/cell/cell.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Cell

***
Cell is library of useful wrappers of values (cells). [pub]

[Cell](#cell) - A wrapper around a mutable value.
Expand Down
2 changes: 1 addition & 1 deletion book/src/libs/convert/convert.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Convert

***
## Infallible

`Infallible` is the error type for errors that can never happen. This can be useful for generic APIs that use Result
Expand Down
2 changes: 1 addition & 1 deletion book/src/libs/iter/iter.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Iter

***
A Rust `Iterator` is analogous to the union of a Dart `Iterable` and `Iterator`. Since Dart already has an `Iterator` class, to avoid confusion,
the Dart implementation of the Rust iterator is `RIterator`. `RIterator`
makes working with collections of `rust_core` types and regular Dart types a breeze. e.g.
Expand Down
2 changes: 1 addition & 1 deletion book/src/libs/option/option.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Option

***
Option represents the union of two types - `Some<T>` and `None`. An `Option<T>` is an extension type of `T?`. Therefore, `Option`
has zero runtime cost and has one big advantage over `T?`, you can chain null specific operations!

Expand Down
1 change: 1 addition & 0 deletions book/src/libs/panic/panic.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Panic
***
As with `Error` in Dart Core, `Panic` represents a state that should never happen and thus should never be caught.
Rust vs Dart Error handling terminology:

Expand Down
8 changes: 6 additions & 2 deletions book/src/libs/result/result.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# Result

***
`Result<T, E>` is the type used for returning and propagating errors. It is an alternative to throwing exceptions. It is a sealed type with the variants, `Ok(T)`, representing success and containing a value, and `Err(E)`, representing error and containing an error value.

To better understand the motivation around the `Result` type refer to this [article].
To better understand the motivation around the `Result` type refer to this [article](https://mcmah309.github.io/#/blog/the_result_type_in_dart).

## Example
***
By using the `Result` type, there is no web of `try`/`catch` statements to maintain and hidden control flow bugs, all control flow is defined.
```dart
import 'package:rust_core/result.dart';
Expand Down Expand Up @@ -55,6 +56,7 @@ Result<String,String> processPayment(String user) {
```

## Chaining
***
Effects on a `Result` can be chained in a safe way without
needing to inspect the type.
```dart
Expand All @@ -74,6 +76,7 @@ finalResult.match(
See the [docs] for all methods and extensions.

## Adding Predictable Control Flow To Legacy Dart Code
***
At times, you may need to integrate with legacy code that may throw or code outside your project. To handle these situations you can just wrap the code in a helper function like `guard`
```dart
void main() {
Expand All @@ -91,6 +94,7 @@ this message was thrown but was guarded
```

## Dart Equivalent To The Rust "?" Early Return Operator
***
In Dart, the Rust "?" operator (Early Return Operator) functionality in `x?`, where `x` is a `Result`, can be
accomplished in two ways
### into()
Expand Down
8 changes: 7 additions & 1 deletion book/src/libs/result/tips_and_best_practices.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Tips and Best Practices

## How to Never Unwrap Incorrectly
***
In Rust, as here, it is possible to unwrap values that should not be unwrapped:
```dart
if (x.isErr()) {
Expand All @@ -17,7 +18,7 @@ if (x case Err(:final err)){
```
and vice versa
```dart
if (x case Ok(:final ok){
if (x case Ok(:final ok)){
return ok;
}
```
Expand Down Expand Up @@ -49,6 +50,7 @@ x.match(
We can also use the [Early Return Key Notation](result.md#early-return-key-notation), which is a very powerful idiomatic approach.

## Working with Futures
***
When working with `Future`s it is easy to make a mistake like this
```dart
Future.delayed(Duration(seconds: 1)); // Future not awaited
Expand Down Expand Up @@ -78,10 +80,12 @@ analyzer:
avoid_void_async: error
```
## ToResult and ToResultEager
***
In various circumstances you just want a single `Result` for these times, think `toResult()` or in some cases
`toResultEager()`. These extension methods have been added to make life easier.

## Iterable Result
***
One of these is on `Iterable<Result<S,F>>`, which can turn into a
`Result<List<S>,List<F>>`. Also, there is `.toResultEager()` which can turn into a single `Result<List<S>,F>`.

Expand All @@ -93,6 +97,7 @@ result = [Ok<int,int>(1), Err<int,int>(2), Ok<int,int>(3)].toResultEager();
expect(result.unwrapErr(), 2);
```
## Multiple Results of Different Success Types
***
Sometimes you need to call multiple functions that return `Result`s of different types. You could write something
like this:
```dart
Expand Down Expand Up @@ -135,6 +140,7 @@ switch(result){
This also has a `toResult()` method.

## Pattern Matching vs Early Return Key
***
```dart
void main(){
usingTheEarlyReturnKey();
Expand Down
2 changes: 1 addition & 1 deletion book/src/libs/slice/slice.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Slice

***
A `Slice` is a contiguous sequence of elements in a [List]. Slices are a view into a list without allocating and copying to a new list,
thus slices are more efficient than creating a sub-list, but they do not own their own data. That means shrinking the original list can cause the slices range to become invalid, which may cause an exception.

Expand Down
4 changes: 2 additions & 2 deletions book/src/libs/sync/channels.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
rust_core supports two types of channels, "local" channels (same isolate) and "isolate" channels (different isolates).

## Local Channels

***
`channel` is used for communication between produces and consumers on the **same** isolate. `channel` is
similar to `StreamController` except it buffers data until read and will never throw.
In more detail, `channel` returns a `Sender` and `Receiver`. Each item `T` sent by the `Sender`
Expand Down Expand Up @@ -127,7 +127,7 @@ void main() async {
```

## Isolate Channels

***
`isolateChannel` is used for bi-directional isolate communication. The returned
`Sender` and `Receiver` can communicate with the spawned isolate and
the spawned isolate is passed a `Sender` and `Receiver` to communicate with the original isolate.
Expand Down
2 changes: 1 addition & 1 deletion book/src/libs/sync/mutex.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Mutex

***
`Mutex` is used to ensure that only one task can perform a critical section of code at a time.
Dart being single threaded, means it is less common to need a `Mutex`, but they are still useful e.g. reading and writing to data sources or transactions. `Mutex` uses a fifo model to prevent starvation.

Expand Down
2 changes: 1 addition & 1 deletion book/src/libs/sync/rwlock.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# RwLock

***
`RwLock` is used in critical sections to allow multiple concurrent read operations while ensuring that write operations are exclusive.
Dart being single threaded, means it is less common to need a `RwLock`, but they are still useful e.g. reading and writing to data sources or transactions. `RwLock` uses a fifo model to prevent starvation.

Expand Down
2 changes: 1 addition & 1 deletion book/src/libs/sync/sync.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Sync

***
sync contains useful synchronization utilities like [channel](./channel.md), [Mutex](./mutex.md), and [RwLock](./rwlock.md).
8 changes: 5 additions & 3 deletions book/src/misc/packages_built_on_rust_core.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
| Library | Description |
| ------- | ----------- |
| [anyhow] | Idiomatic error handling capabilities to make your code safer, more maintainable, and errors easier to debug. |
| [rewind] | A logging utility that allows you completely customize what is captured and how your logs look at each logging level. |
| [rust_std] | Implementation of Rust's standard library in Dart. |
| [path_type] | A zero cost path type.
| [tapper] | Extension methods on all types that allow transparent, temporary, inspection/mutation (tapping), transformation (piping), or type conversion. |
| [thiserror] | A library for concisely defining error types (error enums / sealed classes) and their String representation. |
| [vec] | A zero cost vector type.

## Community
| Library | Description |
| [rewind] | A logging utility that allows you completely customize what is captured and how your logs look at each logging level. |
| [thiserror] | A library for concisely defining error types (error enums / sealed classes) and their String representation. |


[anyhow]: https://pub.dev/packages/anyhow
[thiserror]: https://pub.dev/packages/thiserror
Expand Down
Loading

0 comments on commit a99d85f

Please sign in to comment.