Skip to content

Commit 0e36c5f

Browse files
authored
feat(bytestring): split_at method (#619)
1 parent 47f0017 commit 0e36c5f

File tree

16 files changed

+104
-16
lines changed

16 files changed

+104
-16
lines changed

.github/workflows/lint.yml

+29-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ jobs:
4040

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

@@ -67,3 +67,31 @@ jobs:
6767

6868
- name: check external types
6969
run: just check-external-types-all +${{ vars.RUST_VERSION_EXTERNAL_TYPES }}
70+
71+
public-api-diff:
72+
name: Public API Diff
73+
runs-on: ubuntu-latest
74+
steps:
75+
- name: Checkout main branch
76+
uses: actions/checkout@v4
77+
with:
78+
ref: ${{ github.base_ref }}
79+
80+
- name: Checkout PR branch
81+
uses: actions/checkout@v4
82+
83+
- name: Install Rust (${{ vars.RUST_VERSION_API_DIFF }})
84+
uses: actions-rust-lang/[email protected]
85+
with:
86+
toolchain: ${{ vars.RUST_VERSION_API_DIFF }}
87+
88+
- name: Install cargo-public-api
89+
uses: taiki-e/[email protected]
90+
with:
91+
tool: cargo-public-api
92+
93+
- name: Generate API diff
94+
run: |
95+
for f in $(find -mindepth 2 -maxdepth 2 -name Cargo.toml); do
96+
cargo public-api --manifest-path "$f" --simplified diff ${{ github.event.pull_request.base.sha }}..${{ github.sha }}
97+
done

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ members = [
1717
[workspace.package]
1818
license = "MIT OR Apache-2.0"
1919
edition = "2021"
20-
rust-version = "1.70"
20+
rust-version = "1.71.1"
2121

2222
[patch.crates-io]
2323
actix-codec = { path = "actix-codec" }

actix-codec/CHANGES.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Unreleased
44

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

77
## 0.5.2
88

actix-macros/CHANGES.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Unreleased
44

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

77
## 0.2.4
88

actix-rt/CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
- Minimum supported Rust version (MSRV) is now 1.71.
6+
57
## 2.10.0
68

79
- Relax `F`'s bound (`Fn => FnOnce`) on `{Arbiter, System}::with_tokio_rt()` functions.

actix-server/CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
- Minimum supported Rust version (MSRV) is now 1.71.
6+
57
## 2.5.0
68

79
- Update `mio` dependency to `1`.

actix-service/CHANGES.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Unreleased
44

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

77
## 2.0.2
88

actix-tls/CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
- Minimum supported Rust version (MSRV) is now 1.71.
6+
57
## 3.4.0
68

79
- Add `rustls-0_23`, `rustls-0_23-webpki-roots`, and `rustls-0_23-native-roots` crate features.

actix-tracing/CHANGES.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Unreleased
44

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

77
## 0.1.0
88

actix-utils/CHANGES.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Unreleased
44

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

77
## 3.0.1
88

bytestring/CHANGES.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
## Unreleased
44

5-
- Minimum supported Rust version (MSRV) is now 1.70.
5+
## 1.4.0
6+
7+
- Add `ByteString::split_at()` method.
8+
- Minimum supported Rust version (MSRV) is now 1.71.
69

710
## 1.3.1
811

bytestring/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
[package]
22
name = "bytestring"
3-
version = "1.3.1"
3+
version = "1.4.0"
44
description = "A UTF-8 encoded read-only string using `Bytes` as storage"
55
authors = [
66
"Nikolay Kim <[email protected]>",
77
"Rob Ede <[email protected]>",
88
]
9-
keywords = ["string", "bytes", "utf8", "web", "actix"]
9+
keywords = ["string", "bytes", "utf8", "web", "bytestring"]
1010
categories = ["no-std", "web-programming"]
1111
homepage = "https://actix.rs"
1212
repository = "https://github.com/actix/actix-net"

bytestring/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
<!-- prettier-ignore-start -->
66

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

bytestring/src/lib.rs

+53-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use core::{borrow::Borrow, fmt, hash, ops, str};
1717

1818
use bytes::Bytes;
1919

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

@@ -53,7 +53,29 @@ impl ByteString {
5353
Self(src)
5454
}
5555

56-
/// Returns a new byte string that is equivalent to the given `subset`.
56+
/// Divides one bytestring into two at an index, returning both parts.
57+
///
58+
/// # Panics
59+
///
60+
/// Panics if `mid` is not on a UTF-8 code point boundary, or if it is past the end of the last
61+
/// code point of the bytestring.
62+
pub fn split_at(&self, mid: usize) -> (ByteString, ByteString) {
63+
let this: &str = self.as_ref();
64+
let _valid_midpoint_check = this.split_at(mid);
65+
66+
let mut bytes = self.0.clone();
67+
let first = bytes.split_to(mid);
68+
let last = bytes;
69+
70+
unsafe {
71+
(
72+
ByteString::from_bytes_unchecked(first),
73+
ByteString::from_bytes_unchecked(last),
74+
)
75+
}
76+
}
77+
78+
/// Returns a new `ByteString` that is equivalent to the given `subset`.
5779
///
5880
/// When processing a `ByteString` buffer with other tools, one often gets a `&str` which is in
5981
/// fact a slice of the original `ByteString`; i.e., a subset of it. This function turns that
@@ -465,4 +487,33 @@ mod test {
465487
// being a logical subset of the string
466488
ByteString::from_static("foo bar").slice_ref("foo");
467489
}
490+
491+
#[test]
492+
fn split_at() {
493+
let buf = ByteString::from_static("foo bar");
494+
495+
let (first, last) = buf.split_at(0);
496+
assert_eq!(ByteString::from_static(""), first);
497+
assert_eq!(ByteString::from_static("foo bar"), last);
498+
499+
let (first, last) = buf.split_at(4);
500+
assert_eq!(ByteString::from_static("foo "), first);
501+
assert_eq!(ByteString::from_static("bar"), last);
502+
503+
let (first, last) = buf.split_at(7);
504+
assert_eq!(ByteString::from_static("foo bar"), first);
505+
assert_eq!(ByteString::from_static(""), last);
506+
}
507+
508+
#[test]
509+
#[should_panic = "byte index 1 is not a char boundary;"]
510+
fn split_at_invalid_code_point() {
511+
ByteString::from_static("µ").split_at(1);
512+
}
513+
514+
#[test]
515+
#[should_panic = "byte index 9 is out of bounds"]
516+
fn split_at_outside_string() {
517+
ByteString::from_static("foo").split_at(9);
518+
}
468519
}

local-channel/CHANGES.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Unreleased
44

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

77
## 0.1.5
88

local-waker/CHANGES.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Unreleased
44

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

77
## 0.1.4
88

0 commit comments

Comments
 (0)