sokolpezinok / yaroc

Radio Online Control for orienteering
0 stars 0 forks source link

Rewrite parts of the codebase to Rust #5

Open lukipuki opened 5 months ago

lukipuki commented 5 months ago

As of writing this issue, GitHub shows that 10% of the codebase is written in Rust. This number should be higher, as Python has the following disadvantages compared to Rust.

  1. Weak type system: originally designed as a dynamic language, typing was added much later in Python 3.8. Type checking using mypy is still unable to figure out simple things. As this is a hobby project, weak typing support causes valuable time being lost on simple type errors that the compiler should be able to figure.
  2. Unsuitable for programming microcontrollers. There's MicroPython, but it's very basic with a lot of Python features missing. A big advantage of Python -- lots of libraries -- is not present in MicroPython, as libraries have to be rewritten just for MicroPython.

Type system weakness examples

Many libraries are not typed

pyudev, pyserial, gpiozero and other libraries are not typed yet.

Time handling

datetime.now() and datetime.now().astimezone() cannot be subtracted, as one is decorated with a timezone while the other isn't. Doing that results in a runtime error. In Rust, these are different types, so the error is caught already during compilation.

False negatives

Printing MAC address as a number in hex is not caught when the MAC address is already passed in as a string:

mac_addr = "abcdef012345"
f"{mac_addr:012x}"

False positives

On the other hand, there's also false negatives, like the following example.

def f(x: int) -> int | tuple[int, str]:
    if x == 0:
        return (0, "zero")
    return x

a, b = f(0)
c = f(1)

mypy complains that int is not iterable at the line a, b = f(0).