Skip to content

Experimental/returning strs#2

Merged
tgockel merged 10 commits into
trunkfrom
experimental/returning-strs
Apr 29, 2026
Merged

Experimental/returning strs#2
tgockel merged 10 commits into
trunkfrom
experimental/returning-strs

Conversation

@tgockel
Copy link
Copy Markdown
Owner

@tgockel tgockel commented Apr 29, 2026

No description provided.

Using the `usize`s returned from these functions was awkward and prone
to off-by-one errors.  Returning the `str` slice directly to the user
is most likely what they wanted to do anyway. Suffixes are required to
be `str`s as well, since providing an invalid UTF-8 byte sequence would
invalidate the point of this library.
…oc]`

The lead example with `finish_with_or("!", "…")` and its 6-step walkthrough
moves from `src/lib.rs` into `README.md`, replacing the previous
`finish_with("\0")` example. `src/lib.rs` now pulls the README in as crate
documentation via `#![doc = include_str!("../README.md")]`.
We use `core::error::Error`, which did not stabilize in core until 1.81.
Rust 1.81 was released on 2024-09-05, so it is reasonable to require
this.
There were a few typos in the README and on function documentation that
needed cleaning. This also adds a CHANGELOG.md to document the breaking
changes added in 0.2.
The generic was unused expressivity: only `WriteBuf::finish*` produces
`Truncated`, and all sites used `T = &str`. Inlining the lifetime drops
the `Debug + Display` bounds on the `Error` impl and turns the awkward
`get(&self) -> &&str` into `get(&self) -> &'a str`. `take()` is removed
since `&str` is `Copy` and `get()` already returns the inner slice with
lifetime `'a` (not `&self`), so the two are equivalent.
The `WriteBuf::finish` family returns `Result<&'a str, Truncated<'a>>`
where both arms carry a written `&str`. Callers commonly want that
content (and its byte length) regardless of truncation, previously
written as `result.unwrap_or_else(|e| e.get())` followed by `.len()`.

The new sealed trait exposes `written()`, `written_len()`, and
`is_truncated()` on the raw `Result`. The README's `mylib_strerror`
example now uses the trait.
@tgockel tgockel merged commit 658aafb into trunk Apr 29, 2026
1 check passed
@tgockel tgockel deleted the experimental/returning-strs branch April 29, 2026 16:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant