title options:
- A Memory Safety Enigma in the Mojo Standard Library
- Mojo's Dependent Types and Memory Safety
I was doing my normal thing in Mojo, implementing struct extensions, when I came across this function in the standard library:
| #include <memory> | |
| #include <variant> | |
| #include <iostream> | |
| struct Engine { | |
| int fuel; | |
| Engine(int fuel_) : fuel(fuel_) {} | |
| }; |
| #include <memory> | |
| #include <variant> | |
| #include <iostream> | |
| struct Engine { | |
| int fuel; | |
| Engine(int fuel_) : fuel(fuel_) {} | |
| }; |
| struct Navigation: ~Copyable { | |
| var antenna: Bool | |
| } | |
| struct Engine: ~Copyable { | |
| var warp_coils: [Int] | |
| } | |
| enum Variant: ~Copyable { | |
| case navigation(Navigation) |
| #include <memory> | |
| #include <variant> | |
| #include <iostream> | |
| struct Engine { | |
| int fuel; | |
| Engine(int fuel_) : fuel(fuel_) {} | |
| }; |
| import Foundation | |
| class Engine { | |
| var fuel = 100 | |
| } | |
| class Spaceship { | |
| var engine = Engine() | |
| } | |
| func accelerate(_ ship: Spaceship) { | |
| let engine = ship.engine // Race: read | |
| // literally no field access |
| #include <memory> | |
| #include <thread> | |
| struct Engine { | |
| int fuel = 100; | |
| }; | |
| struct Spaceship { | |
| std::shared_ptr<Engine> engine = std::make_shared<Engine>(); | |
| }; | |
| void accelerate(std::shared_ptr<Spaceship> ship) { | |
| std::shared_ptr<Engine> engine = ship->engine; |
| struct Navigation: ~Copyable { | |
| var antenna: Bool | |
| } | |
| struct Engine: ~Copyable { | |
| var fuel: Int | |
| } | |
| enum Variant: ~Copyable { | |
| case navigation(Navigation) |
| # Article Skeleton | |
| title options: | |
| - A Memory Safety Enigma in the Mojo Standard Library | |
| - Mojo's Dependent Types and Memory Safety | |
| -------- | |
| I was doing my normal thing in Mojo, implementing struct extensions, when I came across this function in the standard library: |