Merge pull request #204 from vitiral/rc_settings
reference count settings to bump performance a bit
This commit is contained in:
commit
ea0c2296e6
42
src/ser.rs
42
src/ser.rs
|
@ -30,6 +30,7 @@ use std::cell::Cell;
|
||||||
use std::error;
|
use std::error;
|
||||||
use std::fmt::{self, Write};
|
use std::fmt::{self, Write};
|
||||||
use std::marker;
|
use std::marker;
|
||||||
|
use std::rc::Rc;
|
||||||
|
|
||||||
use serde::ser;
|
use serde::ser;
|
||||||
use datetime::{SERDE_STRUCT_FIELD_NAME, SERDE_STRUCT_NAME};
|
use datetime::{SERDE_STRUCT_FIELD_NAME, SERDE_STRUCT_NAME};
|
||||||
|
@ -166,12 +167,18 @@ impl ArraySettings {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[doc(hidden)]
|
||||||
|
#[derive(Debug, Default, Clone)]
|
||||||
|
/// String settings. Currently empty but may contain settings
|
||||||
|
/// eventually.
|
||||||
|
struct StringSettings();
|
||||||
|
|
||||||
#[derive(Debug, Default, Clone)]
|
#[derive(Debug, Default, Clone)]
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
/// Internal struct for holding serialization settings
|
/// Internal struct for holding serialization settings
|
||||||
struct Settings {
|
struct Settings {
|
||||||
array: Option<ArraySettings>,
|
array: Option<ArraySettings>,
|
||||||
pretty_string: bool,
|
string: Option<StringSettings>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Serialization implementation for TOML.
|
/// Serialization implementation for TOML.
|
||||||
|
@ -186,7 +193,7 @@ struct Settings {
|
||||||
pub struct Serializer<'a> {
|
pub struct Serializer<'a> {
|
||||||
dst: &'a mut String,
|
dst: &'a mut String,
|
||||||
state: State<'a>,
|
state: State<'a>,
|
||||||
settings: Settings,
|
settings: Rc<Settings>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
|
@ -234,7 +241,7 @@ impl<'a> Serializer<'a> {
|
||||||
Serializer {
|
Serializer {
|
||||||
dst: dst,
|
dst: dst,
|
||||||
state: State::End,
|
state: State::End,
|
||||||
settings: Settings::default(),
|
settings: Rc::new(Settings::default()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -250,16 +257,17 @@ impl<'a> Serializer<'a> {
|
||||||
Serializer {
|
Serializer {
|
||||||
dst: dst,
|
dst: dst,
|
||||||
state: State::End,
|
state: State::End,
|
||||||
settings: Settings {
|
settings: Rc::new(Settings {
|
||||||
array: Some(ArraySettings::pretty()),
|
array: Some(ArraySettings::pretty()),
|
||||||
pretty_string: true,
|
string: Some(StringSettings()),
|
||||||
},
|
}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Enable or Disable pretty strings
|
/// Enable or Disable pretty strings
|
||||||
///
|
///
|
||||||
/// If enabled, strings with one or more newline character will use the `'''` syntax.
|
/// If enabled, literal strings will be used when possible and strings with
|
||||||
|
/// one or more newlines will use triple quotes (i.e.: `'''` or `"""`)
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
|
@ -280,7 +288,11 @@ impl<'a> Serializer<'a> {
|
||||||
/// '''
|
/// '''
|
||||||
/// ```
|
/// ```
|
||||||
pub fn pretty_string(&mut self, value: bool) -> &mut Self {
|
pub fn pretty_string(&mut self, value: bool) -> &mut Self {
|
||||||
self.settings.pretty_string = value;
|
Rc::get_mut(&mut self.settings).unwrap().string = if value {
|
||||||
|
Some(StringSettings())
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -312,7 +324,7 @@ impl<'a> Serializer<'a> {
|
||||||
/// ]
|
/// ]
|
||||||
/// ```
|
/// ```
|
||||||
pub fn pretty_array(&mut self, value: bool) -> &mut Self {
|
pub fn pretty_array(&mut self, value: bool) -> &mut Self {
|
||||||
self.settings.array = if value {
|
Rc::get_mut(&mut self.settings).unwrap().array = if value {
|
||||||
Some(ArraySettings::pretty())
|
Some(ArraySettings::pretty())
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
|
@ -324,7 +336,8 @@ impl<'a> Serializer<'a> {
|
||||||
///
|
///
|
||||||
/// See `Serializer::pretty_array` for more details.
|
/// See `Serializer::pretty_array` for more details.
|
||||||
pub fn pretty_array_indent(&mut self, value: usize) -> &mut Self {
|
pub fn pretty_array_indent(&mut self, value: usize) -> &mut Self {
|
||||||
let use_default = if let &mut Some(ref mut a) = &mut self.settings.array {
|
let use_default = if let &mut Some(ref mut a) = &mut Rc::get_mut(&mut self.settings)
|
||||||
|
.unwrap().array {
|
||||||
a.indent = value;
|
a.indent = value;
|
||||||
false
|
false
|
||||||
} else {
|
} else {
|
||||||
|
@ -334,7 +347,7 @@ impl<'a> Serializer<'a> {
|
||||||
if use_default {
|
if use_default {
|
||||||
let mut array = ArraySettings::pretty();
|
let mut array = ArraySettings::pretty();
|
||||||
array.indent = value;
|
array.indent = value;
|
||||||
self.settings.array = Some(array);
|
Rc::get_mut(&mut self.settings).unwrap().array = Some(array);
|
||||||
}
|
}
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
@ -343,7 +356,8 @@ impl<'a> Serializer<'a> {
|
||||||
///
|
///
|
||||||
/// See `Serializer::pretty_array` for more details.
|
/// See `Serializer::pretty_array` for more details.
|
||||||
pub fn pretty_array_trailing_comma(&mut self, value: bool) -> &mut Self {
|
pub fn pretty_array_trailing_comma(&mut self, value: bool) -> &mut Self {
|
||||||
let use_default = if let &mut Some(ref mut a) = &mut self.settings.array {
|
let use_default = if let &mut Some(ref mut a) = &mut Rc::get_mut(&mut self.settings)
|
||||||
|
.unwrap().array {
|
||||||
a.trailing_comma = value;
|
a.trailing_comma = value;
|
||||||
false
|
false
|
||||||
} else {
|
} else {
|
||||||
|
@ -353,7 +367,7 @@ impl<'a> Serializer<'a> {
|
||||||
if use_default {
|
if use_default {
|
||||||
let mut array = ArraySettings::pretty();
|
let mut array = ArraySettings::pretty();
|
||||||
array.trailing_comma = value;
|
array.trailing_comma = value;
|
||||||
self.settings.array = Some(array);
|
Rc::get_mut(&mut self.settings).unwrap().array = Some(array);
|
||||||
}
|
}
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
@ -533,7 +547,7 @@ impl<'a> Serializer<'a> {
|
||||||
Repr::Literal(out, ty)
|
Repr::Literal(out, ty)
|
||||||
}
|
}
|
||||||
|
|
||||||
let repr = if !is_key && self.settings.pretty_string {
|
let repr = if !is_key && self.settings.string.is_some() {
|
||||||
do_pretty(value)
|
do_pretty(value)
|
||||||
} else {
|
} else {
|
||||||
Repr::Std(Type::OnelineSingle)
|
Repr::Std(Type::OnelineSingle)
|
||||||
|
|
Loading…
Reference in a new issue