From eb5fa30ada30d68ab3320a7e745a33364e9777ba Mon Sep 17 00:00:00 2001 From: Marcus Griep Date: Fri, 10 Jun 2022 23:22:34 -0400 Subject: [PATCH] feat: impls for Box and String conversions (#458) Co-authored-by: Rob Ede --- bytestring/CHANGES.md | 3 +++ bytestring/src/lib.rs | 20 +++++++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/bytestring/CHANGES.md b/bytestring/CHANGES.md index e5d8d41e..14f2cde2 100644 --- a/bytestring/CHANGES.md +++ b/bytestring/CHANGES.md @@ -2,6 +2,9 @@ ## Unreleased - 2022-xx-xx - Minimum supported Rust version (MSRV) is now 1.49. +- Implement `From>` and `Into`. [#458] + +[#458]: https://github.com/actix/actix-net/pull/458 ## 1.0.0 - 2020-12-31 diff --git a/bytestring/src/lib.rs b/bytestring/src/lib.rs index 95a21870..39a153d2 100644 --- a/bytestring/src/lib.rs +++ b/bytestring/src/lib.rs @@ -6,7 +6,11 @@ extern crate alloc; -use alloc::{string::String, vec::Vec}; +use alloc::{ + boxed::Box, + string::{String, ToString}, + vec::Vec, +}; use core::{borrow, convert::TryFrom, fmt, hash, ops, str}; use bytes::Bytes; @@ -110,6 +114,20 @@ impl From<&str> for ByteString { } } +impl From> for ByteString { + #[inline] + fn from(value: Box) -> Self { + Self(Bytes::from(value.into_boxed_bytes())) + } +} + +impl From for String { + #[inline] + fn from(value: ByteString) -> Self { + value.to_string() + } +} + impl TryFrom<&[u8]> for ByteString { type Error = str::Utf8Error;