Skip to content

Commit a5fc8a0

Browse files
bors[bot]matklad
andauthored
23: modernize r=matklad a=matklad Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
2 parents 8854e07 + 56f34ee commit a5fc8a0

File tree

9 files changed

+194
-124
lines changed

9 files changed

+194
-124
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
use std::{
2+
env, fs,
3+
process::{self, Command, ExitStatus, Stdio},
4+
time::Instant,
5+
};
6+
7+
type Error = Box<dyn std::error::Error>;
8+
type Result<T> = std::result::Result<T, Error>;
9+
10+
fn main() {
11+
if let Err(err) = try_main() {
12+
eprintln!("{}", err);
13+
process::exit(1);
14+
}
15+
}
16+
17+
fn try_main() -> Result<()> {
18+
let cwd = env::current_dir()?;
19+
let cargo_toml = cwd.join("Cargo.toml");
20+
assert!(
21+
cargo_toml.exists(),
22+
"Cargo.toml not found, cwd: {}",
23+
cwd.display()
24+
);
25+
26+
{
27+
let _s = Section::new("BUILD");
28+
shell("cargo test --all-features --workspace --no-run")?;
29+
}
30+
31+
{
32+
let _s = Section::new("TEST");
33+
shell("cargo test --all-features --workspace")?;
34+
}
35+
36+
let current_branch = shell_output("git branch --show-current")?;
37+
if &current_branch == "master" {
38+
let _s = Section::new("PUBLISH");
39+
let manifest = fs::read_to_string(&cargo_toml)?;
40+
let version = get_field(&manifest, "version")?;
41+
let tag = format!("v{}", version);
42+
let tags = shell_output("git tag --list")?;
43+
44+
if !tags.contains(&tag) {
45+
let token = env::var("CRATES_IO_TOKEN").unwrap();
46+
shell(&format!("git tag v{}", version))?;
47+
shell(&format!("cargo publish --token {}", token))?;
48+
shell("git push --tags")?;
49+
}
50+
}
51+
Ok(())
52+
}
53+
54+
fn get_field<'a>(text: &'a str, name: &str) -> Result<&'a str> {
55+
for line in text.lines() {
56+
let words = line.split_ascii_whitespace().collect::<Vec<_>>();
57+
match words.as_slice() {
58+
[n, "=", v, ..] if n.trim() == name => {
59+
assert!(v.starts_with('"') && v.ends_with('"'));
60+
return Ok(&v[1..v.len() - 1]);
61+
}
62+
_ => (),
63+
}
64+
}
65+
Err(format!("can't find `{}` in\n----\n{}\n----\n", name, text))?
66+
}
67+
68+
fn shell(cmd: &str) -> Result<()> {
69+
let status = command(cmd).status()?;
70+
check_status(status)
71+
}
72+
73+
fn shell_output(cmd: &str) -> Result<String> {
74+
let output = command(cmd).stderr(Stdio::inherit()).output()?;
75+
check_status(output.status)?;
76+
let res = String::from_utf8(output.stdout)?;
77+
let res = res.trim().to_string();
78+
println!("{}", res);
79+
Ok(res)
80+
}
81+
82+
fn command(cmd: &str) -> Command {
83+
eprintln!("> {}", cmd);
84+
let words = cmd.split_ascii_whitespace().collect::<Vec<_>>();
85+
let (cmd, args) = words.split_first().unwrap();
86+
let mut res = Command::new(cmd);
87+
res.args(args);
88+
res
89+
}
90+
91+
fn check_status(status: ExitStatus) -> Result<()> {
92+
if !status.success() {
93+
Err(format!("$status: {}", status))?;
94+
}
95+
Ok(())
96+
}
97+
98+
struct Section {
99+
name: &'static str,
100+
start: Instant,
101+
}
102+
103+
impl Section {
104+
fn new(name: &'static str) -> Section {
105+
println!("::group::{}", name);
106+
let start = Instant::now();
107+
Section { name, start }
108+
}
109+
}
110+
111+
impl Drop for Section {
112+
fn drop(&mut self) {
113+
eprintln!("{}: {:.2?}", self.name, self.start.elapsed());
114+
println!("::endgroup::");
115+
}
116+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: CI
2+
on:
3+
pull_request:
4+
push:
5+
branches:
6+
- master
7+
- staging
8+
- trying
9+
10+
env:
11+
CARGO_INCREMENTAL: 0
12+
CARGO_NET_RETRY: 10
13+
CI: 1
14+
RUST_BACKTRACE: short
15+
RUSTFLAGS: -D warnings
16+
RUSTUP_MAX_RETRIES: 10
17+
18+
jobs:
19+
rust:
20+
name: Rust
21+
runs-on: ubuntu-latest
22+
23+
steps:
24+
- name: Checkout repository
25+
uses: actions/checkout@v2
26+
with:
27+
fetch-depth: 0
28+
29+
- name: Install Rust toolchain
30+
uses: actions-rs/toolchain@v1
31+
with:
32+
toolchain: stable
33+
profile: minimal
34+
override: true
35+
36+
- run: rustc ./.github/ci.rs && ./ci
37+
env:
38+
CRATES_IO_TOKEN: ${{ secrets.CRATES_IO_TOKEN }}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
target/
1+
/target
2+
/ci
23
Cargo.lock

src/tools/rust-analyzer/lib/smol_str/.travis.yml

Lines changed: 0 additions & 4 deletions
This file was deleted.
Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
11
[package]
22
name = "smol_str"
3-
version = "0.1.16"
4-
authors = ["Aleksey Kladov <aleksey.kladov@gmail.com>"]
5-
repository = "https://github.com/matklad/smol_str"
3+
version = "0.1.17"
64
description = "small-string optimized string type with O(1) clone"
75
license = "MIT OR Apache-2.0"
6+
repository = "https://github.com/matklad/smol_str"
7+
authors = ["Aleksey Kladov <aleksey.kladov@gmail.com>"]
88
edition = "2018"
99

1010
[dependencies]
1111
serde = { version = "1", optional = true, default_features = false, features = [ "std" ] }
1212

1313
[dev-dependencies]
14-
proptest = "0.8.3"
14+
proptest = "0.10"
1515
serde_json = "1"
1616
serde = { version = "1", features = [ "derive" ] }
17-
criterion = "0.2"
18-
19-
[[bench]]
20-
name = "building"
21-
harness = false

src/tools/rust-analyzer/lib/smol_str/benches/building.rs

Lines changed: 0 additions & 44 deletions
This file was deleted.
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
1-
status = [
2-
"continuous-integration/travis-ci/push",
3-
]
1+
status = [ "Rust" ]
42
delete_merged_branches = true

src/tools/rust-analyzer/lib/smol_str/src/lib.rs

Lines changed: 18 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -25,67 +25,7 @@ use std::{
2525
pub struct SmolStr(Repr);
2626

2727
impl SmolStr {
28-
/// Constructs an inline variant of `SmolStr` at compile time.
29-
///
30-
/// # Parameters
31-
///
32-
/// - `len`: Must be short (≤ 22 bytes)
33-
/// - `bytes`: Must be ASCII bytes, and there must be at least `len` of
34-
/// them. If `len` is smaller than the actual len of `bytes`, the string
35-
/// is truncated.
36-
///
37-
/// # Returns
38-
///
39-
/// A constant `SmolStr` with inline data.
40-
///
41-
/// # Examples
42-
///
43-
/// ```rust
44-
/// # use smol_str::SmolStr;
45-
/// const IDENT: SmolStr = SmolStr::new_inline_from_ascii(5, b"hello");
46-
/// ```
47-
///
48-
/// Given a `len` smaller than the number of bytes in `bytes`, the string is
49-
/// cut off:
50-
///
51-
/// ```rust
52-
/// # use smol_str::SmolStr;
53-
/// const SHORT: SmolStr = SmolStr::new_inline_from_ascii(5, b"hello world");
54-
/// assert_eq!(SHORT.as_str(), "hello");
55-
/// ```
56-
///
57-
/// ## Compile-time errors
58-
///
59-
/// This will **fail** at compile-time with a message like "index out of
60-
/// bounds" on a `_len_is_short` because the string is too large:
61-
///
62-
/// ```rust,compile_fail
63-
/// # use smol_str::SmolStr;
64-
/// const IDENT: SmolStr = SmolStr::new_inline_from_ascii(
65-
/// 49,
66-
/// b"hello world, how are you doing this fine morning?",
67-
/// );
68-
/// ```
69-
///
70-
/// Similarly, this will **fail** to compile with "index out of bounds" on
71-
/// an `_is_ascii` binding because it contains non-ASCII characters:
72-
///
73-
/// ```rust,compile_fail
74-
/// # use smol_str::SmolStr;
75-
/// const IDENT: SmolStr = SmolStr::new_inline_from_ascii(
76-
/// 2,
77-
/// &[209, 139],
78-
/// );
79-
/// ```
80-
///
81-
/// Last but not least, given a `len` that is larger than the number of
82-
/// bytes in `bytes`, it will fail to compile with "index out of bounds: the
83-
/// len is 5 but the index is 5" on a binding called `byte`:
84-
///
85-
/// ```rust,compile_fail
86-
/// # use smol_str::SmolStr;
87-
/// const IDENT: SmolStr = SmolStr::new_inline_from_ascii(10, b"hello");
88-
/// ```
28+
#[deprecated = "Use `new_inline` instead"]
8929
pub const fn new_inline_from_ascii(len: usize, bytes: &[u8]) -> SmolStr {
9030
let _len_is_short = [(); INLINE_CAP + 1][len];
9131

@@ -108,6 +48,23 @@ impl SmolStr {
10848
})
10949
}
11050

51+
/// Constructs inline variant of `SmolStr`.
52+
///
53+
/// Panics if `text.len() > 22`.
54+
#[inline]
55+
pub const fn new_inline(text: &str) -> SmolStr {
56+
let mut buf = [0; INLINE_CAP];
57+
let mut i = 0;
58+
while i < text.len() {
59+
buf[i] = text.as_bytes()[i];
60+
i += 1
61+
}
62+
SmolStr(Repr::Inline {
63+
len: text.len() as u8,
64+
buf,
65+
})
66+
}
67+
11168
pub fn new<T>(text: T) -> SmolStr
11269
where
11370
T: AsRef<str>,

src/tools/rust-analyzer/lib/smol_str/tests/test.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
#[macro_use]
2-
extern crate proptest;
1+
use proptest::{prop_assert, prop_assert_eq, proptest};
32

43
use smol_str::SmolStr;
54

@@ -27,6 +26,20 @@ fn conversions() {
2726

2827
#[test]
2928
fn const_fn_ctor() {
29+
const EMPTY: SmolStr = SmolStr::new_inline("");
30+
const A: SmolStr = SmolStr::new_inline("A");
31+
const HELLO: SmolStr = SmolStr::new_inline("HELLO");
32+
const LONG: SmolStr = SmolStr::new_inline("ABCDEFGHIZKLMNOPQRSTUV");
33+
34+
assert_eq!(EMPTY, SmolStr::from(""));
35+
assert_eq!(A, SmolStr::from("A"));
36+
assert_eq!(HELLO, SmolStr::from("HELLO"));
37+
assert_eq!(LONG, SmolStr::from("ABCDEFGHIZKLMNOPQRSTUV"));
38+
}
39+
40+
#[allow(deprecated)]
41+
#[test]
42+
fn old_const_fn_ctor() {
3043
const EMPTY: SmolStr = SmolStr::new_inline_from_ascii(0, b"");
3144
const A: SmolStr = SmolStr::new_inline_from_ascii(1, b"A");
3245
const HELLO: SmolStr = SmolStr::new_inline_from_ascii(5, b"HELLO");

0 commit comments

Comments
 (0)