Skip to content

Commit 49e19df

Browse files
Minimize msrv using a feature controlled macro
1 parent e3caaa3 commit 49e19df

6 files changed

Lines changed: 93 additions & 43 deletions

File tree

.github/workflows/ci.yml

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
on:
22
push:
3-
branches: [master]
3+
branches: [ master ]
44
pull_request:
5-
branches: [master]
5+
branches: [ master ]
66

77
name: Continuous integration
88

@@ -17,21 +17,21 @@ jobs:
1717
strategy:
1818
matrix:
1919
include:
20-
- rust: 1.83.0 # MSRV
20+
- rust: 1.57.0 # MSRV
2121
features: serde
2222
experimental: false
23-
- rust: 1.85.0
24-
features: serde
23+
- rust: 1.70.0
24+
features: serde, const
2525
experimental: false
2626
- rust: stable
27-
features:
27+
features: const
2828
bench: true
2929
experimental: false
3030
- rust: beta
31-
features: serde
31+
features: serde, const
3232
experimental: false
3333
- rust: nightly
34-
features: serde, borsh, zeroize
34+
features: serde, borsh, zeroize, const
3535
experimental: false
3636

3737
steps:
@@ -40,7 +40,7 @@ jobs:
4040
with:
4141
toolchain: ${{ matrix.rust }}
4242
- name: Pin versions for MSRV
43-
if: "${{ matrix.rust == '1.83.0' }}"
43+
if: "${{ matrix.rust == '1.57.0' }}"
4444
run: |
4545
cargo update -p serde_test --precise 1.0.163
4646
cargo update -p serde --precise 1.0.69
@@ -78,6 +78,7 @@ jobs:
7878
run: |
7979
cargo rustc "--target=${{ matrix.target }}" --no-default-features --features "${{ matrix.features }}"
8080
81+
8182
miri:
8283
runs-on: ubuntu-latest
8384
steps:

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ version = "0.7.6"
44
authors = ["bluss"]
55
license = "MIT OR Apache-2.0"
66
edition = "2018"
7-
rust-version = "1.83"
7+
rust-version = "1.57"
88

99
description = "A vector with fixed capacity, backed by an array (it can be stored on the stack too). Implements fixed capacity ArrayVec and ArrayString."
1010
documentation = "https://docs.rs/arrayvec/"
@@ -48,14 +48,15 @@ harness = false
4848
[features]
4949
default = ["std"]
5050
std = []
51+
const = []
5152

5253
[profile.bench]
5354
debug = true
5455
[profile.release]
5556
debug = true
5657

5758
[package.metadata.docs.rs]
58-
features = ["borsh", "serde", "zeroize"]
59+
features = ["borsh", "serde", "zeroize", "const"]
5960

6061
[package.metadata.release]
6162
no-dev-version = true

src/array_string.rs

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use crate::CapacityError;
1717
use crate::LenUint;
1818
use crate::char::encode_utf8;
1919
use crate::utils::MakeMaybeUninit;
20+
use crate::const_fn;
2021

2122
#[cfg(feature="serde")]
2223
use serde::{Serialize, Deserialize, Serializer, Deserializer};
@@ -50,6 +51,8 @@ impl<const CAP: usize> Default for ArrayString<CAP>
5051

5152
impl<const CAP: usize> ArrayString<CAP>
5253
{
54+
55+
const_fn!{
5356
/// Create a new empty `ArrayString`.
5457
///
5558
/// Capacity is inferred from the type parameter.
@@ -67,7 +70,7 @@ impl<const CAP: usize> ArrayString<CAP>
6770
unsafe {
6871
ArrayString { xs: MaybeUninit::uninit().assume_init(), len: 0 }
6972
}
70-
}
73+
}}
7174

7275
/// Create a new empty `ArrayString` (const fn).
7376
///
@@ -91,6 +94,7 @@ impl<const CAP: usize> ArrayString<CAP>
9194
#[inline]
9295
pub const fn is_empty(&self) -> bool { self.len() == 0 }
9396

97+
const_fn!{
9498
/// Create a new `ArrayString` from a `str`.
9599
///
96100
/// Capacity is inferred from the type parameter.
@@ -111,8 +115,9 @@ impl<const CAP: usize> ArrayString<CAP>
111115
Ok(()) => Ok(arraystr),
112116
Err(e) => Err(e),
113117
}
114-
}
118+
}}
115119

120+
const_fn!{
116121
/// Create a new `ArrayString` from a byte string literal.
117122
///
118123
/// **Errors** if the byte string literal is not valid UTF-8.
@@ -135,8 +140,9 @@ impl<const CAP: usize> ArrayString<CAP>
135140
vec.set_len(CAP);
136141
}
137142
Ok(vec)
138-
}
143+
}}
139144

145+
const_fn!{
140146
/// Create a new `ArrayString` value fully filled with ASCII NULL characters (`\0`). Useful
141147
/// to be used as a buffer to collect external data or as a buffer for intermediate processing.
142148
///
@@ -157,7 +163,7 @@ impl<const CAP: usize> ArrayString<CAP>
157163
len: CAP as _
158164
}
159165
}
160-
}
166+
}}
161167

162168
/// Return the capacity of the `ArrayString`.
163169
///
@@ -214,6 +220,7 @@ impl<const CAP: usize> ArrayString<CAP>
214220
self.try_push(c).unwrap();
215221
}
216222

223+
const_fn!{
217224
/// Adds the given char to the end of the string.
218225
///
219226
/// Returns `Ok` if the push succeeds.
@@ -245,7 +252,7 @@ impl<const CAP: usize> ArrayString<CAP>
245252
Err(_) => Err(CapacityError::new(c)),
246253
}
247254
}
248-
}
255+
}}
249256

250257
/// Adds the given string slice to the end of the string.
251258
///
@@ -266,6 +273,7 @@ impl<const CAP: usize> ArrayString<CAP>
266273
self.try_push_str(s).unwrap()
267274
}
268275

276+
const_fn!{
269277
/// Adds the given string slice to the end of the string.
270278
///
271279
/// Returns `Ok` if the push succeeds.
@@ -298,7 +306,7 @@ impl<const CAP: usize> ArrayString<CAP>
298306
self.set_len(newl);
299307
}
300308
Ok(())
301-
}
309+
}}
302310

303311
/// Removes the last character from the string and returns it.
304312
///
@@ -392,13 +400,15 @@ impl<const CAP: usize> ArrayString<CAP>
392400
ch
393401
}
394402

403+
const_fn!{
395404
/// Make the string empty.
396405
pub const fn clear(&mut self) {
397406
unsafe {
398407
self.set_len(0);
399408
}
400-
}
409+
}}
401410

411+
const_fn!{
402412
/// Set the strings’s length.
403413
///
404414
/// This function is `unsafe` because it changes the notion of the
@@ -410,34 +420,37 @@ impl<const CAP: usize> ArrayString<CAP>
410420
// type invariant that capacity always fits in LenUint
411421
debug_assert!(length <= self.capacity());
412422
self.len = length as LenUint;
413-
}
423+
}}
414424

425+
const_fn!{
415426
/// Return a string slice of the whole `ArrayString`.
416427
pub const fn as_str(&self) -> &str {
417428
unsafe {
418429
let sl = slice::from_raw_parts(self.as_ptr(), self.len());
419430
str::from_utf8_unchecked(sl)
420431
}
421-
}
432+
}}
422433

434+
const_fn!{
423435
/// Return a mutable string slice of the whole `ArrayString`.
424436
pub const fn as_mut_str(&mut self) -> &mut str {
425437
unsafe {
426438
let len = self.len();
427439
let sl = slice::from_raw_parts_mut(self.as_mut_ptr(), len);
428440
str::from_utf8_unchecked_mut(sl)
429441
}
430-
}
442+
}}
431443

432444
/// Return a raw pointer to the string's buffer.
433445
pub const fn as_ptr(&self) -> *const u8 {
434446
self.xs.as_ptr() as *const u8
435447
}
436448

449+
const_fn!{
437450
/// Return a raw mutable pointer to the string's buffer.
438451
pub const fn as_mut_ptr(&mut self) -> *mut u8 {
439452
self.xs.as_mut_ptr() as *mut u8
440-
}
453+
}}
441454
}
442455

443456
impl<const CAP: usize> Deref for ArrayString<CAP>

0 commit comments

Comments
 (0)