@@ -149,18 +149,36 @@ impl<'a> Value<'a> {
149149 } )
150150 }
151151
152- pub fn write_string ( & self , f : & mut Vec < u8 > ) {
152+ pub fn write_string ( & self , out : & mut Vec < u8 > ) {
153153 match self {
154- Self :: String ( s) | Self :: Regex ( s) => f . extend_from_slice ( s) ,
155- Self :: Float ( n) => write ! ( f , "{n}" ) . unwrap ( ) ,
156- Self :: Int ( n) => write ! ( f , "{n}" ) . unwrap ( ) ,
157- & Self :: Bool ( false ) => f . push ( b'0' ) ,
158- & Self :: Bool ( true ) => f . push ( b'1' ) ,
154+ Self :: String ( s) | Self :: Regex ( s) => out . extend_from_slice ( s) ,
155+ Self :: Float ( n) => write ! ( out , "{n}" ) . unwrap ( ) ,
156+ Self :: Int ( n) => write ! ( out , "{n}" ) . unwrap ( ) ,
157+ & Self :: Bool ( false ) => out . push ( b'0' ) ,
158+ & Self :: Bool ( true ) => out . push ( b'1' ) ,
159159 Self :: Array ( _) => panic ! ( "Attempted to use array in scalar context!" ) ,
160160 _ => { }
161161 }
162162 }
163163
164+ /// Efficiently replaces the contents of `out` with the string form of the
165+ /// value. Always reuses either buffer.
166+ pub fn move_string_into ( self , out : & mut Vec < u8 > ) {
167+ match self {
168+ Value :: String ( Cow :: Owned ( s) ) => {
169+ * out = s;
170+ }
171+ Value :: String ( Cow :: Borrowed ( s) ) => {
172+ out. clear ( ) ;
173+ out. extend_from_slice ( s) ;
174+ }
175+ _ => {
176+ out. clear ( ) ;
177+ self . write_string ( out) ;
178+ }
179+ }
180+ }
181+
164182 pub fn string_size_hint ( & self ) -> usize {
165183 match self {
166184 Self :: String ( s) | Self :: Regex ( s) => s. len ( ) ,
0 commit comments