earthstar-project / willow-rs

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

Path + PathBuf #8

Closed sgwilym closed 2 weeks ago

sgwilym commented 3 weeks 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...

This PR contains changes to satisfy option 3 (str and String : Path and PathBuf), in case we decide to go that way.

See #7 for the alternative

sgwilym commented 2 weeks ago

Closed in favor of #7