Skip to content

uapi16: split Name into Name and NameBuf - #6

Merged
brauner merged 2 commits into
mainfrom
name-pair
Nov 24, 2026
Merged

brauner merged 2 commits into
mainfrom
name-pair

Conversation

@brauner

@brauner brauner commented Nov 24, 2026

Copy link
Copy Markdown
Member

Two API changes that have to go in before 0.1.0 is published, since
both change what 0.1.0 would ship. Nothing is published yet, so let's
do them now.

  • The Decoder's step() becomes process() and its end() becomes
    end_feed(), which pairs with feed(). The manifest builder is going to
    get operations that are done in steps, driven with process() and
    closed with end(). The Decoder's end() doesn't close anything, it
    only says that the input ended. No functional changes.
  • Name is split into the borrowed Name and the owned NameBuf, the way
    std pairs Path and PathBuf. Today Name::ROOT is a constant of a type
    that has something to drop, so &Name::ROOT is a temporary, and
    anything that keeps a borrowed name past its statement fails with
    E0716. The operations in steps borrow the name they are given for as
    long as they run, so they would hit this right away. Functions that
    read a name keep taking a &Name and the ones that keep a name take a
    NameBuf. Name::new() checks a &str without a copy, and NameBuf::new()
    fails with a NameError that hands the string back. Name::ROOT is a
    &'static Name. While at it, Display pads and truncates a name as it
    does a str.

This adds the crate's first unsafe code: the one cast from a &str to a
&Name, in a single private function. It's the same cast std has for
Path, and the rest of the crate keeps deny(unsafe_code).

Every commit builds and passes clippy and the tests with rustc 1.85 and
the current stable, and for the cross targets, musl and i686. The name
tests pass under Miri, the brute-force one in a reduced form. ufat's
switch branch builds and passes its tests against this with nine lines
changed.

We're going to add operations that are done in steps. They are driven
with process() and closed with end(). Make the naming of the Decoder
consistent with that before we publish it:

- Decoder::step() -> Decoder::process()
- Decoder::end()  -> Decoder::end_feed()

The Decoder's end() doesn't close anything. It only says that the input
ended. So it becomes end_feed() which pairs with feed(). process() still
returns a Step.

No functional changes.

Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
Currently, a Name owns its string and Name::ROOT is a constant of that
type. A constant of a type that has something to drop isn't promoted to
a static. So &Name::ROOT is a reference to a temporary that is dropped
at the end of its statement. Whatever keeps a borrowed name for longer
than that fails to compile with E0716, "temporary value dropped while
borrowed", unless the caller puts the root into a variable of its own
first. The operations in steps that we're going to add borrow the name
they are given for as long as they run. So they would hit this right
away.

We haven't published anything yet so let's do the correct thing and do
what std does for String and str, PathBuf and Path, CString and CStr.
Add an unsized type for a borrowed name next to the type that owns one,
let the functions take the borrowed form and make the root a &'static
of it.

Name becomes the borrowed type and NameBuf is the owned one, as Path
and PathBuf are, since a name is a path. The borrowed type is the one
nearly every function takes. This way their signatures, Name::ROOT and
Name::MAX_LEN keep their spelling.

What changes for callers:

- A function that reads a name takes a &Name as before. A &NameBuf is a
  &Name wherever one is taken.
- A function that keeps a name takes a NameBuf. These are
  FileObject::new(), FileObject::from_metadata() and
  FileObject::set_name(). ContentsSource::File holds one, and
  FileObject::into_name() and Name::join() return one.
- Name::new() checks a &str where it is and returns a &Name without a
  copy. It fails with a NameProblem as before.
- NameBuf::new() makes an owned name. If the string isn't a name it
  fails with the new NameError, which says why and hands the string
  back as NulError and FromUtf8Error do. FromStr and TryFrom for
  NameBuf fail with it as well.
- Name::parent() returns an Option<&Name> as Path::parent() does. The
  name of the directory a file is in is the first part of its own name
  so nothing is copied.
- Name::ROOT is a &'static Name and the default of a &Name. An owned
  root is NameBuf::default().
- Both forms are equal, ordered and hashed the same. So a map with
  NameBuf keys is looked up with a &Name. They compare with each other
  as PathBuf and Path do, and == takes a str with either one on the
  left. There is no order against a str as names aren't in the order
  of their strings. Both still print as Name("a/b") with Debug.

While at it, drop the crate's own Name::parent_str(). It gave the one
caller that didn't want a copy the parent as a string and parent()
does that now. Also make Display pad and truncate a name as it does a
str.

Rust has no safe way to turn a &str into a reference to an unsized type
around a str. It takes a pointer cast and std has the same one for
Path. The cast is in the private Name::from_str_unchecked() and that is
the only function that is allowed to have unsafe code. The crate keeps
deny(unsafe_code). Note that the cast is sound for any string so the
checks of a name are no part of its safety.

Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
@brauner
brauner merged commit 25138e6 into main Nov 24, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant