Skip to content

Commit e2601c4

Browse files
Fix test errors
1 parent 3d84640 commit e2601c4

File tree

2 files changed

+62
-11
lines changed

2 files changed

+62
-11
lines changed

benches/cow_bench.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
use criterion::{criterion_group, criterion_main, Criterion};
2+
use std::hint::black_box;
3+
4+
use heapless::{
5+
cow::CowStr,
6+
string::{String, StringView},
7+
};
8+
9+
fn bench_cowstr(c: &mut Criterion) {
10+
// Owned string
11+
let mut owned_buf: String<32> = String::new();
12+
owned_buf.push_str("owned hello").unwrap();
13+
let owned_s: String<32> = owned_buf;
14+
15+
// Borrowed string (temporary buffer)
16+
let mut temp_buf: String<32> = String::new();
17+
temp_buf.push_str("hello").unwrap();
18+
19+
// Static string
20+
let mut static_buf: String<32> = String::new();
21+
static_buf.push_str("static hello").unwrap();
22+
let static_sv: &'static StringView<usize> = Box::leak(Box::new(static_buf)).as_view();
23+
24+
// Borrowed benchmark
25+
c.bench_function("CowStr::Borrowed to_owned", |b| {
26+
b.iter(|| {
27+
let sv = temp_buf.as_view();
28+
let cow: CowStr<32> = CowStr::Borrowed(sv);
29+
black_box(cow.to_owned());
30+
});
31+
});
32+
33+
// Static benchmark
34+
c.bench_function("CowStr::Static to_owned", |b| {
35+
b.iter(|| {
36+
let cow: CowStr<32> = CowStr::Static(static_sv);
37+
black_box(cow.to_owned());
38+
});
39+
});
40+
41+
// Owned benchmark
42+
c.bench_function("CowStr::Owned clone", |b| {
43+
b.iter(|| {
44+
let cow: CowStr<32> = CowStr::Owned(owned_s.clone());
45+
black_box(cow.to_owned());
46+
});
47+
});
48+
}
49+
50+
criterion_group!(benches, bench_cowstr);
51+
criterion_main!(benches);

src/cow.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -117,16 +117,14 @@ where
117117
#[cfg(test)]
118118
mod tests {
119119
use super::*;
120-
121-
// Helper function to create a StringView from a static str
122-
fn sv_from_str(s: &'static str) -> StringView<usize> {
123-
StringView::new(s)
124-
}
120+
use crate::string::StringView;
121+
use crate::String;
125122

126123
#[test]
127124
fn test_borrowed_variant() {
128-
let view = sv_from_str("hello");
129-
let cow: CowStr<16> = CowStr::Borrowed(&view);
125+
let s: String<16> = String::try_from("hello").unwrap();
126+
let view = s.as_view();
127+
let cow: CowStr<16> = CowStr::Borrowed(view);
130128

131129
assert!(cow.is_borrowed());
132130
assert!(!cow.is_static());
@@ -139,8 +137,9 @@ mod tests {
139137

140138
#[test]
141139
fn test_static_variant() {
142-
static VIEW: StringView<usize> = StringView::new("world");
143-
let cow: CowStr<16> = CowStr::from_static(&VIEW);
140+
let s: String<16> = String::try_from("world").unwrap();
141+
let view: &'static StringView<usize> = Box::leak(Box::new(s));
142+
let cow: CowStr<16> = CowStr::Static(view.as_view());
144143

145144
assert!(!cow.is_borrowed());
146145
assert!(cow.is_static());
@@ -167,8 +166,9 @@ mod tests {
167166

168167
#[test]
169168
fn test_from_stringview() {
170-
let view = sv_from_str("from_borrowed");
171-
let cow: CowStr<16> = CowStr::from(&view);
169+
let s: String<16> = String::try_from("from_borrowed").unwrap();
170+
let view = s.as_view();
171+
let cow: CowStr<16> = CowStr::from(view);
172172

173173
assert!(cow.is_borrowed());
174174
assert_eq!(cow.as_str(), "from_borrowed");

0 commit comments

Comments
 (0)