earthstar-project / willow-rs

Implementations of the Willow family of specifications in Rust.
https://willowprotocol.org
Apache License 2.0
25 stars 1 forks source link

Make Path immutable #7

Closed sgwilym closed 3 months ago

sgwilym commented 3 months ago

@AljoschaMeyer:

we reached the first interesting decisions in the design of our Willow rust codebase. Basically: which sort of ownership model should the trait for Paths assume? Options we see:

  1. A Path trait whose implementors are cheaply clonable, immutable values. Featuring signatures such as Path::append(&self, component: component) -> Self and Path::make_prefix(&self, i: usize) -> Self.
  2. A Path trait whose implementors are owned, mutable values. Featuring signatures such as Path::append(&mut self, component: component) and Path::as_prefix(&self, i: usize) -> &Self.
  3. Two separate traits: a trait Path for immutable views of a path (think str) and a trait PathBuf for owned, mutating access (think String). Featuring signatures such as PathBuf::append(&mut self, component) and Path::as_prefix(&self, i: usize) -> Self, and with the PathBuf implementing AsRef for its associated type of Paths.

Option 2 turns out to be impossible:

Here's a problem with option 2: you can't implement it. Imagine trying to write a function that takes a ref to a Vec and returns a ref to a prefix of that Vec - as a &Vec. Just doesn't work, you cannot store the length of the new vec anywhere with the correct lifetime. So Option 1 or 3 it is...

Option 3 is quite complex.

This PR contains changes to satisfy option 1 (clonable, immutable paths), in case we decide to go that way.

See #8 for the alternative