Skip to content

Commit

Permalink
Bump deps
Browse files Browse the repository at this point in the history
  • Loading branch information
Veykril committed Sep 2, 2024
1 parent fde86a5 commit 8286442
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 105 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/target
/ci
Cargo.lock
/.vscode
Cargo.lock
13 changes: 7 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
99 changes: 1 addition & 98 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -769,101 +769,4 @@ where
}

#[cfg(feature = "serde")]
mod serde {
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)
}
}
}
mod serde;
96 changes: 96 additions & 0 deletions src/serde.rs
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)
}
}

0 comments on commit 8286442

Please sign in to comment.