From a80e1f837063cde8cb8d7cdca48267a70d8e3e93 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Sun, 22 Dec 2019 16:24:28 +0400 Subject: [PATCH] fix new() method and make from_static and from_bytes_unchecked methods const --- string/CHANGES.md | 10 ++++++++-- string/Cargo.toml | 5 ++--- string/src/lib.rs | 18 ++++++++++++------ 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/string/CHANGES.md b/string/CHANGES.md index 10c8d426..7d0a4b84 100644 --- a/string/CHANGES.md +++ b/string/CHANGES.md @@ -1,9 +1,15 @@ # Changes -[0.1.1] - 2019-12-07 +## [0.1.2] - 2019-12-22 + +* Fix `new()` method + +* Make `ByteString::from_static()` and `ByteString::from_bytes_unchecked()` methods const. + +## [0.1.1] - 2019-12-07 * Fix hash impl -[0.1.0] - 2019-12-07 +## [0.1.0] - 2019-12-07 * Initial release diff --git a/string/Cargo.toml b/string/Cargo.toml index 69b9688c..f38c6992 100644 --- a/string/Cargo.toml +++ b/string/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "bytestring" -version = "0.1.1" +version = "0.1.2" authors = ["Nikolay Kim "] description = "A UTF-8 encoded string with Bytes as a storage" keywords = ["actix"] @@ -9,11 +9,10 @@ repository = "https://github.com/actix/actix-net.git" documentation = "https://docs.rs/bytestring/" license = "MIT/Apache-2.0" edition = "2018" -workspace = ".." [lib] name = "bytestring" path = "src/lib.rs" [dependencies] -bytes = "0.5.2" +bytes = "0.5.3" diff --git a/string/src/lib.rs b/string/src/lib.rs index ed3a4896..d6d48ffb 100644 --- a/string/src/lib.rs +++ b/string/src/lib.rs @@ -6,14 +6,14 @@ use bytes::Bytes; /// A utf-8 encoded string with [`Bytes`] as a storage. /// -/// [`Bytes`]: https://docs.rs/bytes/0.5.2/bytes/struct.Bytes.html +/// [`Bytes`]: https://docs.rs/bytes/0.5.3/bytes/struct.Bytes.html #[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Default)] pub struct ByteString(Bytes); impl ByteString { /// Creates a new `ByteString`. - pub fn new() -> String { - String::default() + pub fn new() -> Self { + ByteString(Bytes::new()) } /// Get a reference to the underlying bytes object. @@ -27,12 +27,12 @@ impl ByteString { } /// Creates a new `ByteString` from a static str. - pub fn from_static(src: &'static str) -> ByteString { - Self(Bytes::from_static(src.as_ref())) + pub const fn from_static(src: &'static str) -> ByteString { + Self(Bytes::from_static(src.as_bytes())) } /// Creates a new `ByteString` from a Bytes. - pub unsafe fn from_bytes_unchecked(src: Bytes) -> ByteString { + pub const unsafe fn from_bytes_unchecked(src: Bytes) -> ByteString { Self(src) } } @@ -147,6 +147,11 @@ mod test { use std::collections::hash_map::DefaultHasher; use std::hash::{Hash, Hasher}; + #[test] + fn test_new() { + let _: ByteString = ByteString::new(); + } + #[test] fn test_hash() { let mut hasher1 = DefaultHasher::default(); @@ -171,6 +176,7 @@ mod test { #[test] fn test_from_static_str() { + const _S: ByteString = ByteString::from_static("hello"); let _ = ByteString::from_static("str"); }