1
0
mirror of https://github.com/fafhrd91/actix-net synced 2024-11-27 16:52:58 +01:00

Add PartialEq<T: AsRef<str>>, AsRef<[u8]> impls

This commit is contained in:
Nikolay Kim 2020-01-13 11:58:31 +06:00
parent 7c5fa25b23
commit 3048073919
3 changed files with 25 additions and 1 deletions

View File

@ -1,5 +1,9 @@
# Changes
## [0.1.3] - 2020-01-13
* Add `PartialEq<T: AsRef<str>>`, `AsRef<[u8]>` impls
## [0.1.2] - 2019-12-22
* Fix `new()` method

View File

@ -1,6 +1,6 @@
[package]
name = "bytestring"
version = "0.1.2"
version = "0.1.3"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
description = "A UTF-8 encoded string with Bytes as a storage"
keywords = ["actix"]

View File

@ -43,6 +43,18 @@ impl PartialEq<str> for ByteString {
}
}
impl<T: AsRef<str>> PartialEq<T> for ByteString {
fn eq(&self, other: &T) -> bool {
&self[..] == other.as_ref()
}
}
impl AsRef<[u8]> for ByteString {
fn as_ref(&self) -> &[u8] {
self.0.as_ref()
}
}
impl hash::Hash for ByteString {
fn hash<H: hash::Hasher>(&self, state: &mut H) {
(**self).hash(state);
@ -147,6 +159,14 @@ mod test {
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
#[test]
fn test_partial_eq() {
let s: ByteString = ByteString::from_static("test");
assert_eq!(s, "test");
assert_eq!(s, *"test");
assert_eq!(s, "test".to_string());
}
#[test]
fn test_new() {
let _: ByteString = ByteString::new();