Experimental/returning strs#2
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.