mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-12-27 16:07:46 +00:00
Merge pull request rust-analyzer/smol_str#55 from rust-analyzer/as-ref
This commit is contained in:
commit
b04898eb32
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "smol_str"
|
||||
version = "0.1.25"
|
||||
version = "0.2.0"
|
||||
description = "small-string optimized string type with O(1) clone"
|
||||
license = "MIT OR Apache-2.0"
|
||||
repository = "https://github.com/rust-analyzer/smol_str"
|
||||
|
||||
@ -7,7 +7,7 @@
|
||||
|
||||
A `SmolStr` is a string type that has the following properties:
|
||||
|
||||
* `size_of::<SmolStr>() == size_of::<String>()`
|
||||
* `size_of::<SmolStr>() == 24 (therefore == size_of::<String>() on 64 bit platforms)
|
||||
* `Clone` is `O(1)`
|
||||
* Strings are stack-allocated if they are:
|
||||
* Up to 23 bytes long
|
||||
|
||||
@ -2,6 +2,8 @@
|
||||
extern crate alloc;
|
||||
|
||||
use alloc::{
|
||||
borrow::Cow,
|
||||
boxed::Box,
|
||||
string::{String, ToString},
|
||||
sync::Arc,
|
||||
};
|
||||
@ -17,7 +19,7 @@ use core::{
|
||||
|
||||
/// A `SmolStr` is a string type that has the following properties:
|
||||
///
|
||||
/// * `size_of::<SmolStr>() == size_of::<String>()`
|
||||
/// * `size_of::<SmolStr>() == 24 (therefor == size_of::<String>() on 64 bit platforms)
|
||||
/// * `Clone` is `O(1)`
|
||||
/// * Strings are stack-allocated if they are:
|
||||
/// * Up to 23 bytes long
|
||||
@ -290,22 +292,64 @@ impl<'a> iter::FromIterator<&'a str> for SmolStr {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> From<T> for SmolStr
|
||||
where
|
||||
T: AsRef<str>,
|
||||
{
|
||||
fn from(text: T) -> Self {
|
||||
impl AsRef<str> for SmolStr {
|
||||
#[inline(always)]
|
||||
fn as_ref(&self) -> &str {
|
||||
self.as_str()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&str> for SmolStr {
|
||||
#[inline]
|
||||
fn from(s: &str) -> SmolStr {
|
||||
SmolStr::new(s)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&mut str> for SmolStr {
|
||||
#[inline]
|
||||
fn from(s: &mut str) -> SmolStr {
|
||||
SmolStr::new(s)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&String> for SmolStr {
|
||||
#[inline]
|
||||
fn from(s: &String) -> SmolStr {
|
||||
SmolStr::new(s)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<String> for SmolStr {
|
||||
#[inline(always)]
|
||||
fn from(text: String) -> Self {
|
||||
Self::new(text)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Box<str>> for SmolStr {
|
||||
#[inline]
|
||||
fn from(s: Box<str>) -> SmolStr {
|
||||
SmolStr::new(s)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<Cow<'a, str>> for SmolStr {
|
||||
#[inline]
|
||||
fn from(s: Cow<'a, str>) -> SmolStr {
|
||||
SmolStr::new(s)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<SmolStr> for String {
|
||||
#[inline(always)]
|
||||
fn from(text: SmolStr) -> Self {
|
||||
text.as_str().into()
|
||||
}
|
||||
}
|
||||
|
||||
impl Borrow<str> for SmolStr {
|
||||
#[inline(always)]
|
||||
fn borrow(&self) -> &str {
|
||||
self.as_str()
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user