aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAlex Crichton <alex@alexcrichton.com>2017-07-31 10:58:48 -0500
committerGitHub <noreply@github.com>2017-07-31 10:58:48 -0500
commitea251b375316fab8c9c87726751d9a3014b38ed7 (patch)
tree7c4f2f1981f4f849541ebac1d424e5a6aaf0674f /src
parent5ccd015912a61a7e1df8256beed0a75baefaf13d (diff)
parent09c0afaebe639522bd4e85e3fcd90f6d7ff5860e (diff)
downloadmilf-rs-ea251b375316fab8c9c87726751d9a3014b38ed7.tar.gz
milf-rs-ea251b375316fab8c9c87726751d9a3014b38ed7.zip
Merge pull request #204 from vitiral/rc_settings
reference count settings to bump performance a bit
Diffstat (limited to 'src')
-rw-r--r--src/ser.rs42
1 files changed, 28 insertions, 14 deletions
diff --git a/src/ser.rs b/src/ser.rs
index b055f28..13d4584 100644
--- a/src/ser.rs
+++ b/src/ser.rs
@@ -30,6 +30,7 @@ use std::cell::Cell;
use std::error;
use std::fmt::{self, Write};
use std::marker;
+use std::rc::Rc;
use serde::ser;
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)]
#[doc(hidden)]
/// Internal struct for holding serialization settings
struct Settings {
array: Option<ArraySettings>,
- pretty_string: bool,
+ string: Option<StringSettings>,
}
/// Serialization implementation for TOML.
@@ -186,7 +193,7 @@ struct Settings {
pub struct Serializer<'a> {
dst: &'a mut String,
state: State<'a>,
- settings: Settings,
+ settings: Rc<Settings>,
}
#[derive(Debug, Clone)]
@@ -234,7 +241,7 @@ impl<'a> Serializer<'a> {
Serializer {
dst: dst,
state: State::End,
- settings: Settings::default(),
+ settings: Rc::new(Settings::default()),
}
}
@@ -250,16 +257,17 @@ impl<'a> Serializer<'a> {
Serializer {
dst: dst,
state: State::End,
- settings: Settings {
+ settings: Rc::new(Settings {
array: Some(ArraySettings::pretty()),
- pretty_string: true,
- },
+ string: Some(StringSettings()),
+ }),
}
}
/// 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
///
@@ -280,7 +288,11 @@ impl<'a> Serializer<'a> {
/// '''
/// ```
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
}
@@ -312,7 +324,7 @@ impl<'a> Serializer<'a> {
/// ]
/// ```
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())
} else {
None
@@ -324,7 +336,8 @@ impl<'a> Serializer<'a> {
///
/// See `Serializer::pretty_array` for more details.
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;
false
} else {
@@ -334,7 +347,7 @@ impl<'a> Serializer<'a> {
if use_default {
let mut array = ArraySettings::pretty();
array.indent = value;
- self.settings.array = Some(array);
+ Rc::get_mut(&mut self.settings).unwrap().array = Some(array);
}
self
}
@@ -343,7 +356,8 @@ impl<'a> Serializer<'a> {
///
/// See `Serializer::pretty_array` for more details.
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;
false
} else {
@@ -353,7 +367,7 @@ impl<'a> Serializer<'a> {
if use_default {
let mut array = ArraySettings::pretty();
array.trailing_comma = value;
- self.settings.array = Some(array);
+ Rc::get_mut(&mut self.settings).unwrap().array = Some(array);
}
self
}
@@ -533,7 +547,7 @@ impl<'a> Serializer<'a> {
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)
} else {
Repr::Std(Type::OnelineSingle)