11use crate :: {
2- Array , ExpectedType , FunctionArgs , FunctionDefinition , FunctionDefinitionContext ,
2+ Array , Bytes , ExpectedType , FunctionArgs , FunctionDefinition , FunctionDefinitionContext ,
33 FunctionParam , FunctionParamError , GetType , LhsValue , ParserSettings , Type ,
44} ;
5- use std:: { borrow :: Cow , iter:: once} ;
5+ use std:: iter:: once;
66
77/// A function which, given one or more arrays or byte-strings, returns the
88/// concatenation of each of them.
@@ -43,15 +43,15 @@ fn concat_array<'a>(accumulator: Array<'a>, args: FunctionArgs<'_, 'a>) -> Array
4343 Array :: try_from_vec ( val_type, vec) . unwrap ( )
4444}
4545
46- fn concat_bytes < ' a > ( mut accumulator : Cow < ' a , [ u8 ] > , args : FunctionArgs < ' _ , ' a > ) -> Cow < ' a , [ u8 ] > {
46+ fn concat_bytes < ' a > ( mut accumulator : Vec < u8 > , args : FunctionArgs < ' _ , ' a > ) -> Bytes < ' a > {
4747 for arg in args {
4848 match arg {
49- Ok ( LhsValue :: Bytes ( value) ) => accumulator. to_mut ( ) . extend ( value. iter ( ) ) ,
49+ Ok ( LhsValue :: Bytes ( value) ) => accumulator. extend_from_slice ( & value) ,
5050 Err ( Type :: Bytes ) => ( ) ,
5151 _ => ( ) ,
5252 }
5353 }
54- accumulator
54+ accumulator. into ( )
5555}
5656
5757pub ( crate ) const EXPECTED_TYPES : [ ExpectedType ; 2 ] =
@@ -103,7 +103,10 @@ impl FunctionDefinition for ConcatFunction {
103103 return Some ( LhsValue :: Array ( concat_array ( array, args) ) ) ;
104104 }
105105 Ok ( LhsValue :: Bytes ( bytes) ) => {
106- return Some ( LhsValue :: Bytes ( concat_bytes ( bytes, args) ) ) ;
106+ return Some ( LhsValue :: Bytes ( concat_bytes (
107+ bytes. into_owned ( ) . into ( ) ,
108+ args,
109+ ) ) ) ;
107110 }
108111 Err ( _) => ( ) ,
109112 _ => unreachable ! ( ) ,
@@ -124,27 +127,27 @@ mod tests {
124127 #[ test]
125128 fn test_concat_bytes ( ) {
126129 let mut args = vec ! [
127- Ok ( LhsValue :: Bytes ( Cow :: Borrowed ( b"hello" ) ) ) ,
128- Ok ( LhsValue :: Bytes ( Cow :: Borrowed ( b"world" ) ) ) ,
130+ Ok ( LhsValue :: Bytes ( Bytes :: Borrowed ( b"hello" ) ) ) ,
131+ Ok ( LhsValue :: Bytes ( Bytes :: Borrowed ( b"world" ) ) ) ,
129132 ]
130133 . into_iter ( ) ;
131134 assert_eq ! (
132- Some ( LhsValue :: Bytes ( Cow :: Borrowed ( b"helloworld" ) ) ) ,
135+ Some ( LhsValue :: Bytes ( Bytes :: Borrowed ( b"helloworld" ) ) ) ,
133136 CONCAT_FN . compile( & mut std:: iter:: empty( ) , None ) ( & mut args)
134137 ) ;
135138 }
136139
137140 #[ test]
138141 fn test_concat_many_bytes ( ) {
139142 let mut args = vec ! [
140- Ok ( LhsValue :: Bytes ( Cow :: Borrowed ( b"hello" ) ) ) ,
141- Ok ( LhsValue :: Bytes ( Cow :: Borrowed ( b"world" ) ) ) ,
142- Ok ( LhsValue :: Bytes ( Cow :: Borrowed ( b"hello2" ) ) ) ,
143- Ok ( LhsValue :: Bytes ( Cow :: Borrowed ( b"world2" ) ) ) ,
143+ Ok ( LhsValue :: Bytes ( Bytes :: Borrowed ( b"hello" ) ) ) ,
144+ Ok ( LhsValue :: Bytes ( Bytes :: Borrowed ( b"world" ) ) ) ,
145+ Ok ( LhsValue :: Bytes ( Bytes :: Borrowed ( b"hello2" ) ) ) ,
146+ Ok ( LhsValue :: Bytes ( Bytes :: Borrowed ( b"world2" ) ) ) ,
144147 ]
145148 . into_iter ( ) ;
146149 assert_eq ! (
147- Some ( LhsValue :: Bytes ( Cow :: Borrowed ( b"helloworldhello2world2" ) ) ) ,
150+ Some ( LhsValue :: Bytes ( Bytes :: Borrowed ( b"helloworldhello2world2" ) ) ) ,
148151 CONCAT_FN . compile( & mut std:: iter:: empty( ) , None ) ( & mut args)
149152 ) ;
150153 }
0 commit comments