From 06df8e5a47904250739a3751cfeb43a9b897884d Mon Sep 17 00:00:00 2001 From: Garrett Berg Date: Fri, 28 Jul 2017 15:12:39 -0600 Subject: reference count settings to bump performance a bit --- src/ser.rs | 52 +++++++++++++++++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 19 deletions(-) (limited to 'src/ser.rs') diff --git a/src/ser.rs b/src/ser.rs index 721fbf1..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, - pretty_string: bool, + string: Option, } /// 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, } #[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,37 +257,42 @@ 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 /// /// Instead of: /// - /// ```ignore + /// ```toml,ignore /// single = "no newlines" /// text = "\nfoo\nbar\n" /// ``` /// /// You will have: /// - /// ```ignore - /// single = "no newlines" + /// ```toml,ignore + /// single = 'no newlines' /// text = ''' /// foo /// bar /// ''' /// ``` 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 } @@ -299,20 +311,20 @@ impl<'a> Serializer<'a> { /// /// Instead of: /// - /// ```ignore + /// ```toml,ignore /// array = ["foo", "bar"] /// ``` /// /// You will have: /// - /// ```ignore + /// ```toml,ignore /// array = [ /// "foo", /// "bar", /// ] /// ``` 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) -- cgit v1.2.3 From 09c0afaebe639522bd4e85e3fcd90f6d7ff5860e Mon Sep 17 00:00:00 2001 From: Garrett Berg Date: Sun, 30 Jul 2017 10:19:32 -0600 Subject: back out doc changes for now --- src/ser.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src/ser.rs') diff --git a/src/ser.rs b/src/ser.rs index 13d4584..bd13365 100644 --- a/src/ser.rs +++ b/src/ser.rs @@ -273,15 +273,15 @@ impl<'a> Serializer<'a> { /// /// Instead of: /// - /// ```toml,ignore + /// ```ignore /// single = "no newlines" /// text = "\nfoo\nbar\n" /// ``` /// /// You will have: /// - /// ```toml,ignore - /// single = 'no newlines' + /// ```ignore + /// single = "no newlines" /// text = ''' /// foo /// bar @@ -311,13 +311,13 @@ impl<'a> Serializer<'a> { /// /// Instead of: /// - /// ```toml,ignore + /// ```ignore /// array = ["foo", "bar"] /// ``` /// /// You will have: /// - /// ```toml,ignore + /// ```ignore /// array = [ /// "foo", /// "bar", -- cgit v1.2.3