From e8ace6fb0f89b220c0b21ff38616a26211160006 Mon Sep 17 00:00:00 2001 From: Marcus Griep Date: Thu, 9 Jun 2022 09:25:07 -0400 Subject: [PATCH] feat: impls for Box and String conversions --- bytestring/CHANGES.md | 1 + bytestring/src/lib.rs | 20 +++++++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/bytestring/CHANGES.md b/bytestring/CHANGES.md index e5d8d41e..e053e17c 100644 --- a/bytestring/CHANGES.md +++ b/bytestring/CHANGES.md @@ -2,6 +2,7 @@ ## Unreleased - 2022-xx-xx - Minimum supported Rust version (MSRV) is now 1.49. +- Implement `From>` and `Into` ## 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;