Skip to content

Commit 1cea16b

Browse files
authored
Some minor changes for partiql-value (#200)
- Fix `partiql_list` and `partiql_bag` macro_rules for repetitive values - Minor changes to code placement in `lib.rs`
1 parent 00ba504 commit 1cea16b

File tree

1 file changed

+64
-51
lines changed

1 file changed

+64
-51
lines changed

partiql-value/src/lib.rs

Lines changed: 64 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,57 @@ impl Ord for Value {
176176
}
177177
}
178178

179+
impl From<String> for Value {
180+
#[inline]
181+
fn from(s: String) -> Self {
182+
Value::String(Box::new(s))
183+
}
184+
}
185+
186+
impl From<&str> for Value {
187+
#[inline]
188+
fn from(s: &str) -> Self {
189+
Value::String(Box::new(s.to_string()))
190+
}
191+
}
192+
193+
impl From<i64> for Value {
194+
#[inline]
195+
fn from(n: i64) -> Self {
196+
Value::Integer(n)
197+
}
198+
}
199+
200+
impl From<f64> for Value {
201+
#[inline]
202+
fn from(f: f64) -> Self {
203+
Value::Real(OrderedFloat(f))
204+
}
205+
}
206+
207+
impl From<List> for Value {
208+
#[inline]
209+
fn from(v: List) -> Self {
210+
Value::List(Box::new(v))
211+
}
212+
}
213+
214+
impl From<Tuple> for Value {
215+
#[inline]
216+
fn from(v: Tuple) -> Self {
217+
Value::Tuple(Box::new(v))
218+
}
219+
}
220+
221+
impl From<Bag> for Value {
222+
#[inline]
223+
fn from(v: Bag) -> Self {
224+
Value::Bag(Box::new(v))
225+
}
226+
}
227+
179228
#[derive(Default, Hash, PartialEq, Eq, Clone)]
229+
/// Represents a PartiQL List value, e.g. [1, 2, 'one']
180230
pub struct List(Vec<Value>);
181231

182232
impl List {
@@ -226,7 +276,7 @@ macro_rules! partiql_list {
226276
List::from(vec![])
227277
);
228278
($elem:expr; $n:expr) => (
229-
List::from(vec![Value::from($elem), $n])
279+
List::from(vec![Value::from($elem); $n])
230280
);
231281
($($x:expr),+ $(,)?) => (
232282
List::from(vec![$(Value::from($x)),+])
@@ -252,13 +302,6 @@ impl Iterator for ListIntoIterator {
252302
}
253303
}
254304

255-
impl From<List> for Value {
256-
#[inline]
257-
fn from(v: List) -> Self {
258-
Value::List(Box::new(v))
259-
}
260-
}
261-
262305
impl Debug for List {
263306
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
264307
f.debug_list().entries(&self.0).finish()
@@ -307,6 +350,7 @@ impl Ord for List {
307350
}
308351

309352
#[derive(Default, Eq, Clone)]
353+
/// Represents a PartiQL BAG value, e.g.: <<1, 'two', 4>>
310354
pub struct Bag(Vec<Value>);
311355

312356
impl Bag {
@@ -346,7 +390,7 @@ macro_rules! partiql_bag {
346390
Bag::from(vec![])
347391
);
348392
($elem:expr; $n:expr) => (
349-
Bag::from(vec![Value::from($elem), $n])
393+
Bag::from(vec![Value::from($elem); $n])
350394
);
351395
($($x:expr),+ $(,)?) => (
352396
Bag::from(vec![$(Value::from($x)),+])
@@ -372,13 +416,6 @@ impl Iterator for BagIntoIterator {
372416
}
373417
}
374418

375-
impl From<Bag> for Value {
376-
#[inline]
377-
fn from(v: Bag) -> Self {
378-
Value::Bag(Box::new(v))
379-
}
380-
}
381-
382419
impl Debug for Bag {
383420
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
384421
write!(f, "<<")?;
@@ -444,13 +481,6 @@ where
444481
}
445482
}
446483

447-
impl From<Tuple> for Value {
448-
#[inline]
449-
fn from(v: Tuple) -> Self {
450-
Value::Tuple(Box::new(v))
451-
}
452-
}
453-
454484
#[macro_export]
455485
macro_rules! partiql_tuple {
456486
() => (
@@ -517,34 +547,6 @@ impl Hash for Tuple {
517547
}
518548
}
519549

520-
impl From<String> for Value {
521-
#[inline]
522-
fn from(s: String) -> Self {
523-
Value::String(Box::new(s))
524-
}
525-
}
526-
527-
impl From<&str> for Value {
528-
#[inline]
529-
fn from(s: &str) -> Self {
530-
Value::String(Box::new(s.to_string()))
531-
}
532-
}
533-
534-
impl From<i64> for Value {
535-
#[inline]
536-
fn from(n: i64) -> Self {
537-
Value::Integer(n)
538-
}
539-
}
540-
541-
impl From<f64> for Value {
542-
#[inline]
543-
fn from(f: f64) -> Self {
544-
Value::Real(OrderedFloat(f))
545-
}
546-
}
547-
548550
#[cfg(test)]
549551
mod tests {
550552
use super::*;
@@ -578,4 +580,15 @@ mod tests {
578580
println!("Cow<Value> size: {}", mem::size_of::<Cow<Value>>());
579581
println!("Cow<&Value> size: {}", mem::size_of::<Cow<&Value>>());
580582
}
583+
584+
#[test]
585+
fn macro_rules_tests() {
586+
println!("partiql_list:{:?}", partiql_list!());
587+
println!("partiql_list:{:?}", partiql_list![10, 10]);
588+
println!("partiql_list:{:?}", partiql_list!(5; 3));
589+
println!("partiql_bag:{:?}", partiql_bag!());
590+
println!("partiql_bag:{:?}", partiql_bag![10, 10]);
591+
println!("partiql_bag:{:?}", partiql_bag!(5; 3));
592+
println!("partiql_tuple:{:?}", partiql_tuple![("a", 1), ("b", 2)]);
593+
}
581594
}

0 commit comments

Comments
 (0)