-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
106 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
/target | ||
/ci | ||
Cargo.lock | ||
/.vscode | ||
Cargo.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,21 +5,22 @@ description = "small-string optimized string type with O(1) clone" | |
license = "MIT OR Apache-2.0" | ||
repository = "https://github.com/rust-analyzer/smol_str" | ||
authors = ["Aleksey Kladov <[email protected]>"] | ||
edition = "2018" | ||
edition = "2021" | ||
|
||
[package.metadata.docs.rs] | ||
rustdoc-args = ["--cfg", "docsrs"] | ||
all-features = true | ||
|
||
[dependencies] | ||
serde = { version = "1.0.136", optional = true, default-features = false } | ||
arbitrary = { version = "1.1.0", optional = true } | ||
serde = { version = "1.0", optional = true, default-features = false } | ||
arbitrary = { version = "1.3", optional = true } | ||
|
||
[dev-dependencies] | ||
proptest = "1.0.0" | ||
serde_json = "1.0.79" | ||
serde = { version = "1.0.136", features = ["derive"] } | ||
proptest = "1.5" | ||
serde_json = "1.0" | ||
serde = { version = "1.0", features = ["derive"] } | ||
|
||
[features] | ||
default = ["std"] | ||
std = ["serde?/std"] | ||
serde = ["dep:serde"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
use alloc::{string::String, vec::Vec}; | ||
use core::fmt; | ||
|
||
use serde::de::{Deserializer, Error, Unexpected, Visitor}; | ||
|
||
use crate::SmolStr; | ||
|
||
// https://github.com/serde-rs/serde/blob/629802f2abfd1a54a6072992888fea7ca5bc209f/serde/src/private/de.rs#L56-L125 | ||
fn smol_str<'de: 'a, 'a, D>(deserializer: D) -> Result<SmolStr, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
struct SmolStrVisitor; | ||
|
||
impl<'a> Visitor<'a> for SmolStrVisitor { | ||
type Value = SmolStr; | ||
|
||
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { | ||
formatter.write_str("a string") | ||
} | ||
|
||
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> | ||
where | ||
E: Error, | ||
{ | ||
Ok(SmolStr::from(v)) | ||
} | ||
|
||
fn visit_borrowed_str<E>(self, v: &'a str) -> Result<Self::Value, E> | ||
where | ||
E: Error, | ||
{ | ||
Ok(SmolStr::from(v)) | ||
} | ||
|
||
fn visit_string<E>(self, v: String) -> Result<Self::Value, E> | ||
where | ||
E: Error, | ||
{ | ||
Ok(SmolStr::from(v)) | ||
} | ||
|
||
fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E> | ||
where | ||
E: Error, | ||
{ | ||
match core::str::from_utf8(v) { | ||
Ok(s) => Ok(SmolStr::from(s)), | ||
Err(_) => Err(Error::invalid_value(Unexpected::Bytes(v), &self)), | ||
} | ||
} | ||
|
||
fn visit_borrowed_bytes<E>(self, v: &'a [u8]) -> Result<Self::Value, E> | ||
where | ||
E: Error, | ||
{ | ||
match core::str::from_utf8(v) { | ||
Ok(s) => Ok(SmolStr::from(s)), | ||
Err(_) => Err(Error::invalid_value(Unexpected::Bytes(v), &self)), | ||
} | ||
} | ||
|
||
fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Self::Value, E> | ||
where | ||
E: Error, | ||
{ | ||
match String::from_utf8(v) { | ||
Ok(s) => Ok(SmolStr::from(s)), | ||
Err(e) => Err(Error::invalid_value( | ||
Unexpected::Bytes(&e.into_bytes()), | ||
&self, | ||
)), | ||
} | ||
} | ||
} | ||
|
||
deserializer.deserialize_str(SmolStrVisitor) | ||
} | ||
|
||
impl serde::Serialize for SmolStr { | ||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: serde::Serializer, | ||
{ | ||
self.as_str().serialize(serializer) | ||
} | ||
} | ||
|
||
impl<'de> serde::Deserialize<'de> for SmolStr { | ||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | ||
where | ||
D: serde::Deserializer<'de>, | ||
{ | ||
smol_str(deserializer) | ||
} | ||
} |