Skip to content

Instantly share code, notes, and snippets.

View matanlurey's full-sized avatar

Matan Lurey matanlurey

View GitHub Profile
@matanlurey
matanlurey / dart_ffi_stdout.dart
Created May 19, 2024 21:24
A fully working example of using `dart:ffi` to write to `stdout`
import 'dart:ffi';
import 'dart:io' as io show Platform;
void main() {
// Note these instructions were *only* tested on Linux or MacOS.
if (!io.Platform.isLinux && !io.Platform.isMacOS) {
throw UnsupportedError('This code was only tested on Linux and MacOS.');
}
// First, you need access to a dynamic library. The default C library is
@matanlurey
matanlurey / result.dart
Last active September 10, 2023 20:23
Example of a Result type in Dart 3.
sealed class Result<T, E> {
const Result();
const factory Result.value(T value) = ValueResult._;
const factory Result.error(E error) = ErrorResult._;
}
final class ValueResult<T, E> extends Result<T, E> {
final T value;
@matanlurey
matanlurey / flutter-wiki-local-host-engine.diff
Created August 10, 2023 19:25
Show the changes I'm making to the Flutter Wiki to add information about `--local-host-engine`
diff --git a/Comparing-AOT-Snapshot-Sizes.md b/Comparing-AOT-Snapshot-Sizes.md
index 2355d82..a5631cc 100644
--- a/Comparing-AOT-Snapshot-Sizes.md
+++ b/Comparing-AOT-Snapshot-Sizes.md
@@ -1,10 +1,10 @@
These instructions can be used to prepare a tabulated summary of the differences in the sizes of two AOT snapshots. The instructions assume that the Flutter Engine has been [setup](https://github.com/flutter/flutter/wiki/Setting-up-the-Engine-development-environment) on the host (at `FLUTTER_ENGINE` in these instructions).
-Build the AOT snapshot (`flutter build aot`) for the application but pass in the `--verbose` flag. We need to find the `gen_snapshot` invocation and re-run it with an extra option (`--print-instructions-sizes-to`). If you are instrumenting with a local engine, the `flutter build` takes the `--local-engine` flag as well.
+Build the AOT snapshot (`flutter build aot`) for the application but pass in the `--verbose` flag. We need to find the `gen_snapshot` invocation and re-run it with an ex
@matanlurey
matanlurey / ephemeral.dart
Created November 27, 2022 04:41
Example of a short-lived value in Dart.
/// A short-lived [value] `T`, that conditionally is re-computed or re-fetched.
class Ephemeral<T> {
final Future<T> Function() _fetch;
final bool Function(T) _isExpired;
/// Returns [value] by invoking [fetch].
///
/// If [isExpired] returns `false` for a given value, the value is re-fetched.
///
/// ```
@matanlurey
matanlurey / abstract_fields.dart
Created September 28, 2022 02:46
An example of abstract fields in Dart.
// An interface that defines two mutable fields.
abstract class Ball {
abstract int x;
abstract int y;
}
// Implements the fields by initializing in constructor.
class BallImplConstructor implements Ball {
@override
int x;
@matanlurey
matanlurey / ansi.dart
Created September 4, 2022 02:13
A simple example of using Dart's "enhanced" enums for ANSI escape sequences
/// ANSI escape sequence constants.
///
/// See also:
/// - <https://vt100.net/docs/vt100-ug/chapter3.html>.
/// - <https://www.lihaoyi.com/post/BuildyourownCommandLinewithANSIescapecodes.html>
library ansi;
/// Provides constants for 16-bit colors and styles using ANSI-escape sequences.
enum AnsiEscapes16Bit {
/// Resets all styled output after this escape sequence.
@matanlurey
matanlurey / dart-dependency-injection.md
Created August 23, 2022 01:36
Some notes I have on the dependency injection frameworks for Dart/Flutter today

Survey of Dependency Injection Packages for Dart

I searched for "Dependency Injection" and looked at the [top ~10 or so on pub.dev][top-10].

Configuring

class AppModule extends Module {
@matanlurey
matanlurey / enum_to_map.dart
Created August 21, 2022 15:11
Provides an extension method to convert enum values to a map
void main() {
// {Animals.cat: 0, Animals.dog: 1, Animals.pig: 2}
print(Animals.values.toMap());
}
extension EnumHelper<E extends Enum> on Iterable<E> {
Map<E, int> toMap() {
return {for (final v in this) v: v.index};
}
}
@matanlurey
matanlurey / argv.dart
Last active August 1, 2022 04:43
Emulates shell-style string parsing into a list of arguments.
/// Parses a shell-style string [input] into a list of arguments.
///
/// ```dart
/// // [-x, 3, -y, 4, -abc, -beep=boop, foo, bar, baz]
/// print(argv('-x 3 -y 4 -abc -beep=boop foo "bar" \'baz\''));
/// ```
List<String> argv(String input) {
const $space = 0x20;
const $tab = 0x09;
const $newLine = 0x0a;
@matanlurey
matanlurey / main.dart
Created July 18, 2022 00:36
rustic-utopia-8178
void main() {
late final a = Animal();
print('Meow');
print(a.sound);
}
class Animal {
Animal() {
print('CREATED: Animal');
}