Skip to content

Commit 66676b3

Browse files
committed
fix: StrView hash impl
1 parent 86efb04 commit 66676b3

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "byteview"
33
description = "Thin, immutable zero-copy slice type"
44
license = "MIT OR Apache-2.0"
5-
version = "0.5.1"
5+
version = "0.5.2"
66
edition = "2021"
77
rust-version = "1.74"
88
readme = "README.md"

src/strview.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use std::{ops::Deref, sync::Arc};
1515
///
1616
/// Uses [`ByteView`] internally, but derefs as [`&str`].
1717
#[repr(C)]
18-
#[derive(Default, Clone, PartialEq, Eq, PartialOrd, Ord, std::hash::Hash)]
18+
#[derive(Default, Clone, PartialEq, Eq, PartialOrd, Ord)]
1919
pub struct StrView(ByteView);
2020

2121
#[allow(clippy::non_send_fields_in_send_ty)]
@@ -44,6 +44,12 @@ impl Deref for StrView {
4444
}
4545
}
4646

47+
impl std::hash::Hash for StrView {
48+
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
49+
self.deref().hash(state);
50+
}
51+
}
52+
4753
impl StrView {
4854
/// Creates a new string from an existing byte string.
4955
///
@@ -187,6 +193,16 @@ mod serde {
187193
#[cfg(test)]
188194
mod tests {
189195
use super::StrView;
196+
use std::collections::HashMap;
197+
198+
#[test]
199+
fn strview_hash() {
200+
let a = StrView::from("abcdef");
201+
202+
let mut map = HashMap::new();
203+
map.insert(a, 0);
204+
assert!(map.contains_key("abcdef"));
205+
}
190206

191207
#[test]
192208
fn cmp_misc_1() {

0 commit comments

Comments
 (0)