Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented [&]Cow<str> for SharedString #6371

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions internal/core/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ use alloc::string::String;
use core::fmt::{Debug, Display, Write};
use core::ops::Deref;

#[cfg(feature = "std")]
use std::borrow::Cow;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need feature="std" if you use it from alloc:

Suggested change
#[cfg(feature = "std")]
use std::borrow::Cow;
use alloc::borrow::Cow;

then the feature="std" is also not needed later.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Learned something new. Thanks!


/// This macro is the same as [`std::format!`], but it returns a [`SharedString`] instead.
///
/// ### Example
Expand Down Expand Up @@ -212,6 +215,20 @@ impl From<&String> for SharedString {
}
}

#[cfg(feature = "std")]
impl From<Cow<'_, str>> for SharedString {
fn from(s: Cow<'_, str>) -> Self {
s.as_ref().into()
}
}

#[cfg(feature = "std")]
impl From<&Cow<'_, str>> for SharedString {
fn from(s: &Cow<'_, str>) -> Self {
s.as_ref().into()
}
}

impl From<char> for SharedString {
fn from(c: char) -> Self {
SharedString::from(c.encode_utf8(&mut [0; 6]) as &str)
Expand Down
Loading