Skip to content

Commit

Permalink
Replace PrettyConfig's Strings with Cow<'static, str>
Browse files Browse the repository at this point in the history
Signed-off-by: Coca <coca16622@gmail.com>
  • Loading branch information
Coca162 committed Sep 11, 2024
1 parent abc60f5 commit bed5a3c
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 27 deletions.
2 changes: 1 addition & 1 deletion fuzz/fuzz_targets/bench/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ impl From<ArbitraryPrettyConfig> for PrettyConfig {
fn from(arbitrary: ArbitraryPrettyConfig) -> Self {
Self::default()
.depth_limit((arbitrary.depth_limit % 16).into())
.indentor(String::from(" ")) // conserve some memory and time
.indentor(" ") // conserve some memory and time
.struct_names(arbitrary.struct_names)
.separate_tuple_members(arbitrary.separate_tuple_members)
.enumerate_arrays(arbitrary.enumerate_arrays)
Expand Down
30 changes: 15 additions & 15 deletions src/ser/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::fmt;
use std::{borrow::Cow, fmt};

use serde::{ser, ser::Serialize};
use serde_derive::{Deserialize, Serialize};
Expand Down Expand Up @@ -71,7 +71,7 @@ struct Pretty {
/// let my_config = PrettyConfig::new()
/// .depth_limit(4)
/// // definitely superior (okay, just joking)
/// .indentor("\t".to_owned());
/// .indentor("\t");
/// ```
#[allow(clippy::struct_excessive_bools)]
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
Expand All @@ -81,11 +81,11 @@ pub struct PrettyConfig {
/// Limit the pretty-ness up to the given depth.
pub depth_limit: usize,
/// New line string
pub new_line: String,
pub new_line: Cow<'static, str>,
/// Indentation string
pub indentor: String,
pub indentor: Cow<'static, str>,
/// Separator string
pub separator: String,
pub separator: Cow<'static, str>,
// Whether to emit struct names
pub struct_names: bool,
/// Separate tuple members with indentation
Expand Down Expand Up @@ -135,8 +135,8 @@ impl PrettyConfig {
///
/// Default: `\r\n` on Windows, `\n` otherwise
#[must_use]
pub fn new_line(mut self, new_line: String) -> Self {
self.new_line = new_line;
pub fn new_line(mut self, new_line: impl Into<Cow<'static, str>>) -> Self {
self.new_line = new_line.into();

self
}
Expand All @@ -145,8 +145,8 @@ impl PrettyConfig {
///
/// Default: 4 spaces
#[must_use]
pub fn indentor(mut self, indentor: String) -> Self {
self.indentor = indentor;
pub fn indentor(mut self, indentor: impl Into<Cow<'static, str>>) -> Self {
self.indentor = indentor.into();

self
}
Expand All @@ -155,8 +155,8 @@ impl PrettyConfig {
///
/// Default: 1 space
#[must_use]
pub fn separator(mut self, separator: String) -> Self {
self.separator = separator;
pub fn separator(mut self, separator: impl Into<Cow<'static, str>>) -> Self {
self.separator = separator.into();

self
}
Expand Down Expand Up @@ -344,12 +344,12 @@ impl Default for PrettyConfig {
PrettyConfig {
depth_limit: usize::MAX,
new_line: if cfg!(not(target_os = "windows")) {
String::from("\n")
Cow::Borrowed("\n")
} else {
String::from("\r\n") // GRCOV_EXCL_LINE
Cow::Borrowed("\r\n") // GRCOV_EXCL_LINE
},
indentor: String::from(" "),
separator: String::from(" "),
indentor: Cow::Borrowed(" "),
separator: Cow::Borrowed(" "),
struct_names: false,
separate_tuple_members: false,
enumerate_arrays: false,
Expand Down
2 changes: 1 addition & 1 deletion tests/147_empty_sets_serialisation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ fn empty_sets_arrays() {

let pretty = ron::ser::PrettyConfig::new()
.enumerate_arrays(true)
.new_line("\n".to_string());
.new_line("\n");
let serial = ron::ser::to_string_pretty(&value, pretty).unwrap();

println!("Serialized: {}", serial);
Expand Down
10 changes: 5 additions & 5 deletions tests/240_array_pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use ron::ser::{to_string_pretty, PrettyConfig};
fn small_array() {
let arr = &[(), (), ()][..];
assert_eq!(
to_string_pretty(&arr, PrettyConfig::new().new_line("\n".to_string())).unwrap(),
to_string_pretty(&arr, PrettyConfig::new().new_line("\n")).unwrap(),
"[
(),
(),
Expand All @@ -15,7 +15,7 @@ fn small_array() {
to_string_pretty(
&arr,
PrettyConfig::new()
.new_line("\n".to_string())
.new_line("\n")
.compact_arrays(true)
)
.unwrap(),
Expand All @@ -25,9 +25,9 @@ fn small_array() {
to_string_pretty(
&arr,
PrettyConfig::new()
.new_line("\n".to_string())
.new_line("\n")
.compact_arrays(true)
.separator("".to_string())
.separator("")
)
.unwrap(),
"[(),(),()]"
Expand All @@ -36,7 +36,7 @@ fn small_array() {
to_string_pretty(
&vec![(1, 2), (3, 4)],
PrettyConfig::new()
.new_line("\n".to_string())
.new_line("\n")
.separate_tuple_members(true)
.compact_arrays(true)
)
Expand Down
2 changes: 1 addition & 1 deletion tests/depth_limit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ fn depth_limit() {
.depth_limit(1)
.separate_tuple_members(true)
.enumerate_arrays(true)
.new_line("\n".to_string());
.new_line("\n");
let s = ron::ser::to_string_pretty(&data, pretty);

assert_eq!(s, Ok(EXPECTED.to_string()));
Expand Down
2 changes: 1 addition & 1 deletion tests/preserve_sequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn make_roundtrip(source: &str) -> String {
.depth_limit(3)
.separate_tuple_members(true)
.enumerate_arrays(true)
.new_line("\n".into());
.new_line("\n");
to_string_pretty(&config, pretty).expect("Serialization failed")
}

Expand Down
6 changes: 3 additions & 3 deletions tests/unicode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ fn serialize_invalid_whitespace() {
assert_eq!(
ron::ser::to_string_pretty(
&42,
ron::ser::PrettyConfig::default().new_line(String::from("a"))
ron::ser::PrettyConfig::default().new_line("a")
)
.unwrap_err(),
Error::Message(String::from(
Expand All @@ -51,7 +51,7 @@ fn serialize_invalid_whitespace() {
assert_eq!(
ron::ser::to_string_pretty(
&42,
ron::ser::PrettyConfig::default().indentor(String::from("a"))
ron::ser::PrettyConfig::default().indentor("a")
)
.unwrap_err(),
Error::Message(String::from(
Expand All @@ -61,7 +61,7 @@ fn serialize_invalid_whitespace() {
assert_eq!(
ron::ser::to_string_pretty(
&42,
ron::ser::PrettyConfig::default().separator(String::from("a"))
ron::ser::PrettyConfig::default().separator("a")
)
.unwrap_err(),
Error::Message(String::from(
Expand Down

0 comments on commit bed5a3c

Please sign in to comment.