Skip to content

Commit

Permalink
feat(bytestring): split_at method (#619)
Browse files Browse the repository at this point in the history
  • Loading branch information
robjtede authored Nov 24, 2024
1 parent 47f0017 commit 0e36c5f
Show file tree
Hide file tree
Showing 16 changed files with 104 additions and 16 deletions.
30 changes: 29 additions & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:

- uses: giraffate/[email protected]
with:
reporter: 'github-pr-check'
reporter: "github-pr-check"
github_token: ${{ secrets.GITHUB_TOKEN }}
clippy_flags: --workspace --all-features --tests --examples --bins -- -Dclippy::todo -Aunknown_lints

Expand All @@ -67,3 +67,31 @@ jobs:

- name: check external types
run: just check-external-types-all +${{ vars.RUST_VERSION_EXTERNAL_TYPES }}

public-api-diff:
name: Public API Diff
runs-on: ubuntu-latest
steps:
- name: Checkout main branch
uses: actions/checkout@v4
with:
ref: ${{ github.base_ref }}

- name: Checkout PR branch
uses: actions/checkout@v4

- name: Install Rust (${{ vars.RUST_VERSION_API_DIFF }})
uses: actions-rust-lang/[email protected]
with:
toolchain: ${{ vars.RUST_VERSION_API_DIFF }}

- name: Install cargo-public-api
uses: taiki-e/[email protected]
with:
tool: cargo-public-api

- name: Generate API diff
run: |
for f in $(find -mindepth 2 -maxdepth 2 -name Cargo.toml); do
cargo public-api --manifest-path "$f" --simplified diff ${{ github.event.pull_request.base.sha }}..${{ github.sha }}
done
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ members = [
[workspace.package]
license = "MIT OR Apache-2.0"
edition = "2021"
rust-version = "1.70"
rust-version = "1.71.1"

[patch.crates-io]
actix-codec = { path = "actix-codec" }
Expand Down
2 changes: 1 addition & 1 deletion actix-codec/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Unreleased

- Minimum supported Rust version (MSRV) is now 1.70.
- Minimum supported Rust version (MSRV) is now 1.71.

## 0.5.2

Expand Down
2 changes: 1 addition & 1 deletion actix-macros/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Unreleased

- Minimum supported Rust version (MSRV) is now 1.70.
- Minimum supported Rust version (MSRV) is now 1.71.

## 0.2.4

Expand Down
2 changes: 2 additions & 0 deletions actix-rt/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- Minimum supported Rust version (MSRV) is now 1.71.

## 2.10.0

- Relax `F`'s bound (`Fn => FnOnce`) on `{Arbiter, System}::with_tokio_rt()` functions.
Expand Down
2 changes: 2 additions & 0 deletions actix-server/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- Minimum supported Rust version (MSRV) is now 1.71.

## 2.5.0

- Update `mio` dependency to `1`.
Expand Down
2 changes: 1 addition & 1 deletion actix-service/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Unreleased

- Minimum supported Rust version (MSRV) is now 1.70.
- Minimum supported Rust version (MSRV) is now 1.71.

## 2.0.2

Expand Down
2 changes: 2 additions & 0 deletions actix-tls/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- Minimum supported Rust version (MSRV) is now 1.71.

## 3.4.0

- Add `rustls-0_23`, `rustls-0_23-webpki-roots`, and `rustls-0_23-native-roots` crate features.
Expand Down
2 changes: 1 addition & 1 deletion actix-tracing/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Unreleased

- Minimum supported Rust version (MSRV) is now 1.70.
- Minimum supported Rust version (MSRV) is now 1.71.

## 0.1.0

Expand Down
2 changes: 1 addition & 1 deletion actix-utils/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Unreleased

- Minimum supported Rust version (MSRV) is now 1.70.
- Minimum supported Rust version (MSRV) is now 1.71.

## 3.0.1

Expand Down
5 changes: 4 additions & 1 deletion bytestring/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

## Unreleased

- Minimum supported Rust version (MSRV) is now 1.70.
## 1.4.0

- Add `ByteString::split_at()` method.
- Minimum supported Rust version (MSRV) is now 1.71.

## 1.3.1

Expand Down
4 changes: 2 additions & 2 deletions bytestring/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[package]
name = "bytestring"
version = "1.3.1"
version = "1.4.0"
description = "A UTF-8 encoded read-only string using `Bytes` as storage"
authors = [
"Nikolay Kim <[email protected]>",
"Rob Ede <[email protected]>",
]
keywords = ["string", "bytes", "utf8", "web", "actix"]
keywords = ["string", "bytes", "utf8", "web", "bytestring"]
categories = ["no-std", "web-programming"]
homepage = "https://actix.rs"
repository = "https://github.com/actix/actix-net"
Expand Down
4 changes: 2 additions & 2 deletions bytestring/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
<!-- prettier-ignore-start -->

[![crates.io](https://img.shields.io/crates/v/bytestring?label=latest)](https://crates.io/crates/bytestring)
[![Documentation](https://docs.rs/bytestring/badge.svg?version=1.3.1)](https://docs.rs/bytestring/1.3.1)
[![Documentation](https://docs.rs/bytestring/badge.svg?version=1.4.0)](https://docs.rs/bytestring/1.4.0)
[![Version](https://img.shields.io/badge/rustc-1.52+-ab6000.svg)](https://blog.rust-lang.org/2021/05/06/Rust-1.52.0.html)
![MIT or Apache 2.0 licensed](https://img.shields.io/crates/l/bytestring.svg)
<br />
[![Dependency Status](https://deps.rs/crate/bytestring/1.3.1/status.svg)](https://deps.rs/crate/bytestring/1.3.1)
[![Dependency Status](https://deps.rs/crate/bytestring/1.4.0/status.svg)](https://deps.rs/crate/bytestring/1.4.0)
![Download](https://img.shields.io/crates/d/bytestring.svg)
[![Chat on Discord](https://img.shields.io/discord/771444961383153695?label=chat&logo=discord)](https://discord.gg/NWpN5mmg3x)

Expand Down
55 changes: 53 additions & 2 deletions bytestring/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use core::{borrow::Borrow, fmt, hash, ops, str};

use bytes::Bytes;

/// An immutable UTF-8 encoded string with [`Bytes`] as a storage.
/// An immutable UTF-8 encoded string using [`Bytes`] as the storage.
#[derive(Clone, Default, Eq, PartialOrd, Ord)]
pub struct ByteString(Bytes);

Expand Down Expand Up @@ -53,7 +53,29 @@ impl ByteString {
Self(src)
}

/// Returns a new byte string that is equivalent to the given `subset`.
/// Divides one bytestring into two at an index, returning both parts.
///
/// # Panics
///
/// Panics if `mid` is not on a UTF-8 code point boundary, or if it is past the end of the last
/// code point of the bytestring.
pub fn split_at(&self, mid: usize) -> (ByteString, ByteString) {
let this: &str = self.as_ref();
let _valid_midpoint_check = this.split_at(mid);

let mut bytes = self.0.clone();
let first = bytes.split_to(mid);
let last = bytes;

unsafe {
(
ByteString::from_bytes_unchecked(first),
ByteString::from_bytes_unchecked(last),
)
}
}

/// Returns a new `ByteString` that is equivalent to the given `subset`.
///
/// When processing a `ByteString` buffer with other tools, one often gets a `&str` which is in
/// fact a slice of the original `ByteString`; i.e., a subset of it. This function turns that
Expand Down Expand Up @@ -465,4 +487,33 @@ mod test {
// being a logical subset of the string
ByteString::from_static("foo bar").slice_ref("foo");
}

#[test]
fn split_at() {
let buf = ByteString::from_static("foo bar");

let (first, last) = buf.split_at(0);
assert_eq!(ByteString::from_static(""), first);
assert_eq!(ByteString::from_static("foo bar"), last);

let (first, last) = buf.split_at(4);
assert_eq!(ByteString::from_static("foo "), first);
assert_eq!(ByteString::from_static("bar"), last);

let (first, last) = buf.split_at(7);
assert_eq!(ByteString::from_static("foo bar"), first);
assert_eq!(ByteString::from_static(""), last);
}

#[test]
#[should_panic = "byte index 1 is not a char boundary;"]
fn split_at_invalid_code_point() {
ByteString::from_static("µ").split_at(1);
}

#[test]
#[should_panic = "byte index 9 is out of bounds"]
fn split_at_outside_string() {
ByteString::from_static("foo").split_at(9);
}
}
2 changes: 1 addition & 1 deletion local-channel/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Unreleased

- Minimum supported Rust version (MSRV) is now 1.70.
- Minimum supported Rust version (MSRV) is now 1.71.

## 0.1.5

Expand Down
2 changes: 1 addition & 1 deletion local-waker/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Unreleased

- Minimum supported Rust version (MSRV) is now 1.70.
- Minimum supported Rust version (MSRV) is now 1.71.

## 0.1.4

Expand Down

0 comments on commit 0e36c5f

Please sign in to comment.