holmes 0.1.0

Holmes Inference System
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
//! This module provides extensible, dynamically typed persistable values for
//! use in the Holmes language and postgres db.
//! TODO put a demo on custom types in here

use std::hash::{Hash, Hasher};
use std::sync::Arc;

/// `HashTO` is a Trait Object-safe version of the Hash trait, using a
/// reference to a `Hasher` rather than a polymorphic one in order to allow
/// construction of trait objects.
///
/// `HashTO` is implemented automatically for any type implementing `Hash`,
/// allowing Trait Objects to implement `Hash`. For example:
///
/// ```
/// use holmes::pg::dyn::HashTO;
/// use std::hash::{Hash, Hasher};
/// trait Foo : HashTO {}
/// #[derive(Hash)]
/// struct Bar;
/// impl Foo for Bar {}
/// impl Hash for Foo {
///   fn hash<H : Hasher>(&self, hasher : &mut H) {
///     self.hash_to(hasher)
///   }
/// }
/// ```
///
/// This trick allows `Value` and `Type` trait objects to be used as keys in
/// maps by having types implementing the `ValueT` or `TypeT` interface derive
/// `Hash`.
pub trait HashTO {
    /// `hash_to` captures the same functionality as `Hash`'s `hash()`, but in
    /// a trait object safe way.
    fn hash_to(&self, &mut Hasher);
}

struct HashProxy<'a> {
    pub hasher: &'a mut Hasher,
}

impl<'a> Hasher for HashProxy<'a> {
    fn finish(&self) -> u64 {
        self.hasher.finish()
    }
    fn write(&mut self, bytes: &[u8]) {
        self.hasher.write(bytes)
    }
}

impl<T: Hash> HashTO for T {
    fn hash_to(&self, h: &mut Hasher) {
        self.hash(&mut HashProxy { hasher: h })
    }
}

/// Represents the type of a dynamic value as a threadsafe trait object.
pub type Type = Arc<self::types::TypeT>;
/// Represents a dynamic value as a threadsafe trait object.
pub type Value = Arc<self::values::ValueT>;

pub mod types {
    //! This module defines the trait new types must implement, along with
    //! several core types to avoid the need to rewrite basic types every time.
    //! It is heavily codependent on the `values` module.
    use super::values;
    use super::super::RowIter;
    use std::any::Any;
    use std::fmt;
    use std::sync::Arc;
    use std::hash::{Hash, Hasher};
    use super::HashTO;
    use super::Type;
    use super::Value;

    #[macro_export]
    macro_rules! typet_inner {
      () => {
          fn inner(&self) -> &::std::any::Any {
              self as &::std::any::Any
          }
      }
  }

    #[macro_export]
    macro_rules! typet_inner_eq {
      () => {
          fn inner_eq(&self, other : &TypeT) -> bool {
              let other_self = match other.inner().downcast_ref() {
                  Some(x) => x,
                  None => return false
              };
              self == other_self
          }
      }
  }

    #[macro_export]
    macro_rules! typet_boiler {
      () => {
          typet_inner!();
          typet_inner_eq!();
          fn large_unique(&self) -> bool {
              false
          }
      }
  }

    /// The TypeT trait defines the interface a new Holmes type must implement
    /// to be registered.
    pub trait TypeT: Sync + Send + HashTO + Any {
        /// For a registered type, name() will provide the way to add it to a
        /// predicate, and the thing to pattern match against when loading from the
        /// db.
        /// None will be returned if the type is anonymous, such as a tuple or
        /// list.
        fn name(&self) -> Option<&'static str>;
        /// Takes in an iterator over the row, then attempts to read a value of the
        /// type specified.
        fn extract(&self, &mut RowIter) -> Option<Value>;
        /// Generates the database representation of the fields required. For
        /// example,
        /// for a bitvector this would be
        /// ```
        /// vec!["bytea".to_string(), "int64".to_string()]
        /// ```
        /// or similar, depending on how you chose to construct the size.
        fn repr(&self) -> Vec<::std::string::String>;
        /// Returns a dynamic representation of the trait object.
        ///
        /// Trait objects cannot be cast to other trait objects, even if they
        /// implement the other trait necessarily. In order to access functions
        /// under the `Any` trait, I have the non-trait-object implementation
        /// provide access to its &Any representation.
        fn inner(&self) -> &Any;
        /// Check equality
        ///
        /// Similar to `inner`, `inner_eq` exports a `PartialEq` instance from
        /// the underlying type.
        fn inner_eq(&self, other: &TypeT) -> bool;
        /// Should this type be considered when checking for uniqueness of a fact?
        /// This should only be set to true for large fields which have a more
        /// compact representation of the field elsewhere in the tuple, e.g. the
        /// contents of a file and its hash.
        fn large_unique(&self) -> bool;
    }

    impl Hash for TypeT {
        fn hash<H: Hasher>(&self, hasher: &mut H) {
            self.hash_to(hasher)
        }
    }

    impl fmt::Debug for TypeT {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            write!(f, "[Name: {:?}, Repr: {:?}]", self.name(), self.repr())
        }
    }

    impl Eq for TypeT {}
    impl PartialEq for TypeT {
        fn eq(&self, t: &TypeT) -> bool {
            self.inner_eq(t)
        }
    }

    /// The Trap type is used to represent types not yet present in the program,
    /// but present in the database. Most forms of interaction with a trap type
    /// will take down the program.
    #[derive(Debug,Clone,Hash,PartialEq)]
    pub struct Trap;
    impl TypeT for Trap {
        typet_boiler!();
        fn name(&self) -> Option<&'static str> {
            Some("trap")
        }
        fn extract(&self, _rows: &mut RowIter) -> Option<Value> {
            panic!("Tried to extract from a trap")
        }
        fn repr(&self) -> Vec<::std::string::String> {
            panic!("Tried to observe the representation of a trap")
        }
    }
    impl Trap {
        /// Instantiates the Trap type
        pub fn new() -> Arc<Self> {
            Arc::new(Trap)
        }
    }

    /// A tuple of other `Type`s
    /// This type is anonymous.
    #[derive(Debug,Clone,Hash,PartialEq)]
    pub struct Tuple {
        elements: Vec<Type>,
    }

    impl Tuple {
        /// Construct a new tuple from a vector of other types
        pub fn new(elems: Vec<Type>) -> Arc<Self> {
            Arc::new(Tuple { elements: elems })
        }
    }

    impl TypeT for Tuple {
        typet_boiler!();
        fn name(&self) -> Option<&'static str> {
            None
        }
        fn extract(&self, rows: &mut RowIter) -> Option<Value> {
            let mut out = Vec::new();
            for elem in self.elements.iter() {
                match elem.extract(rows) {
                    Some(v) => out.push(v),
                    None => return None,
                }
            }
            Some(values::Tuple::new(out))
        }
        fn repr(&self) -> Vec<::std::string::String> {
            self.elements.iter().flat_map(|elem| elem.repr()).collect()
        }
    }

    /// A list of another `Type`
    /// This type is anonymous.
    #[derive(Debug,Clone,Hash)]
    pub struct List {
        elem: Type,
    }

    impl List {
        /// Constructs a list type of the provided element type
        pub fn new(elem: Type) -> Arc<Self> {
            Arc::new(List { elem: elem })
        }
    }

    // PartialEq won't derive right
    impl PartialEq for List {
        fn eq(&self, other: &Self) -> bool {
            *self.elem == *other.elem
        }
    }

    impl TypeT for List {
        typet_boiler!();
        fn name(&self) -> Option<&'static str> {
            None
        }
        fn extract(&self, _rows: &mut RowIter) -> Option<Value> {
            panic!("List support disabled, will be re-enabled via arrays maybe")
        }
        fn repr(&self) -> Vec<::std::string::String> {
            panic!("List support disabled, will be re-enabled via arrays maybe")
        }
    }

    /// Provides a list of provided named types for use by the database
    /// as a default set of types.
    pub fn default_types() -> Vec<Type> {
        vec![Arc::new(UInt64),
             Arc::new(String),
             Arc::new(Bytes),
             Arc::new(LargeBytes),
             Arc::new(Bool)]
    }

    /// Boolean type
    #[derive(Debug,Clone,Hash,PartialEq)]
    pub struct Bool;
    impl TypeT for Bool {
        typet_boiler!();
        fn name(&self) -> Option<&'static str> {
            Some("bool")
        }
        fn extract(&self, rows: &mut RowIter) -> Option<Value> {
            rows.next().map(|b| values::Bool::new(b) as Value)
        }
        fn repr(&self) -> Vec<::std::string::String> {
            vec!["bool".to_string()]
        }
    }

    /// Unsigned 64-bit int type
    #[derive(Debug,Clone,Hash,PartialEq)]
    pub struct UInt64;

    impl TypeT for UInt64 {
        typet_boiler!();
        fn name(&self) -> Option<&'static str> {
            Some("uint64")
        }
        fn extract(&self, rows: &mut RowIter) -> Option<Value> {
            rows.next().map(|x: i64| values::UInt64::new(x as u64) as Value)
        }
        fn repr(&self) -> Vec<::std::string::String> {
            vec!["int8".to_string()]
        }
    }

    /// `String` type
    /// Use this for text. If you want to store a buffer, use `Bytes` instead.
    #[derive(Debug,Clone,Hash,PartialEq)]
    pub struct String;

    impl TypeT for String {
        typet_boiler!();
        fn name(&self) -> Option<&'static str> {
            Some("string")
        }
        fn extract(&self, rows: &mut RowIter) -> Option<Value> {
            rows.next().map(|s| values::String::new(s) as Value)
        }
        fn repr(&self) -> Vec<::std::string::String> {
            vec!["varchar".to_string()]
        }
    }

    /// `Bytes` is for storing raw data
    /// If you want to store text, use the `String` type.
    #[derive(Debug,Clone,Hash,PartialEq)]
    pub struct Bytes;

    impl TypeT for Bytes {
        typet_boiler!();
        fn name(&self) -> Option<&'static str> {
            Some("bytes")
        }
        fn extract(&self, rows: &mut RowIter) -> Option<Value> {
            rows.next().map(|b| values::Bytes::new(b) as Value)
        }
        fn repr(&self) -> Vec<::std::string::String> {
            vec!["bytea".to_string()]
        }
    }

    /// `LargeBytes` is for storing raw data which should not be considered
    /// in uniqueness checks
    /// If you want to store text, use the `String` type.
    #[derive(Debug,Clone,Hash,PartialEq)]
    pub struct LargeBytes;

    impl TypeT for LargeBytes {
        typet_inner!();
        typet_inner_eq!();
        fn large_unique(&self) -> bool {
            true
        }
        fn name(&self) -> Option<&'static str> {
            Some("largebytes")
        }
        fn extract(&self, rows: &mut RowIter) -> Option<Value> {
            rows.next().map(|v| values::Bytes::new(v) as Value)
        }
        fn repr(&self) -> Vec<::std::string::String> {
            vec!["bytea".to_string()]
        }
    }


}

pub mod values {
    //! This module defines the trait new values must implement, along with
    //! several core values to instantiate the basic types provided in `types`.
    //! It is heavily codependent on the `values` module.
    use postgres::types::ToSql;
    use std::any::Any;
    use super::HashTO;
    use std::sync::Arc;
    use std::hash::{Hash, Hasher};
    use std::fmt;
    use super::Type;
    use super::Value;
    use super::types;
    use std::cmp::Ordering;

    #[macro_export]
    macro_rules! valuet_boiler {
      () => {
        fn inner(&self) -> &::std::any::Any {
          self as &::std::any::Any
        }
        fn inner_eq(&self, other : &ValueT) -> bool {
          let other_typed = match other.inner().downcast_ref::<Self>() {
            Some(x) => x,
            None => return false
          };
          self == other_typed
        }
        fn inner_ord(&self, other : &ValueT) -> Option<::std::cmp::Ordering> {
          other.inner().downcast_ref::<Self>().and_then(|other_typed|{
            self.partial_cmp(other_typed)
          })
        }
      }
  }

    /// This trait defines the interface any value must implement in order to be
    /// used in the Holmes language.
    pub trait ValueT: Sync + Send + HashTO + fmt::Debug + fmt::Display + Any {
        /// Returns the type of the value
        /// This is needed if to do type checking, or tuple values
        fn type_(&self) -> Type;
        /// Get a rust dynamic type version of the value contained.
        ///
        /// Since Holmes is a typed language, the user of this function should
        /// know what to expect and be able to cast from Any into it.
        fn get(&self) -> &Any;
        /// Converts the value into a list of ToSql trait objects to allow for
        /// insertion into the database via a prepared query
        fn to_sql(&self) -> Vec<&ToSql>;
        /// Returns a dynamic representation of the trait object.
        ///
        /// Trait objects cannot be cast to other trait objects, even if they
        /// implement the other trait necessarily. In order to access functions
        /// under the `Any` trait, I have the non-trait-object implementation
        /// provide access to its &Any representation.
        fn inner(&self) -> &Any;
        /// Check equality
        ///
        /// Similar to `inner`, `inner_eq` exports a `PartialEq` instance from
        /// the underlying type.
        fn inner_eq(&self, other: &ValueT) -> bool;
        /// Check order
        ///
        /// Similar to `inner`, `inner_ord` exports an `Ord` instance form
        /// the underlying type
        fn inner_ord(&self, &ValueT) -> Option<Ordering>;
    }

    impl Hash for ValueT {
        fn hash<H: Hasher>(&self, hasher: &mut H) {
            self.hash_to(hasher)
        }
    }

    /// Represents the transformability of some type into a dynamic value.
    ///
    /// This is useful both as an easy way to turn Rust values into Holmes
    /// values, and to allow for the use of literals in the macro DSL system.
    pub trait ToValue {
        /// Converts a rust native type into a Holmes `Value`
        fn to_value(self) -> Value;
    }

    impl Eq for ValueT {}
    impl PartialEq for ValueT {
        fn eq(&self, other: &ValueT) -> bool {
            self.inner_eq(other)
        }
    }

    impl PartialOrd for ValueT {
        fn partial_cmp(&self, other: &ValueT) -> Option<Ordering> {
            self.inner_ord(other)
        }
    }

    /// A list of samely typed values.
    #[derive(Debug,Clone,PartialEq,Hash,PartialOrd,Eq)]
    pub struct List {
        elements: Vec<Value>,
    }

    impl fmt::Display for List {
        fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
            write!(fmt, "[")?;
            let mut first = true;
            for elem in self.elements.iter() {
                if !first {
                    write!(fmt, ", ")?;
                    first = false;
                }
                write!(fmt, "{}", elem)?;
            }
            write!(fmt, "]")
        }
    }

    impl ValueT for List {
        fn type_(&self) -> Type {
            match self.elements.first() {
                Some(e) => types::List::new(e.type_()),
                // TODO have some kind of poly type to default to? Equal to everything?
                None => types::List::new(Arc::new(types::UInt64)),
            }
        }
        fn get(&self) -> &Any {
            &self.elements as &Any
        }
        fn to_sql(&self) -> Vec<&ToSql> {
            panic!("List SQL disabled")
        }
        valuet_boiler!();
    }

    impl List {
        /// Create a dynamic `List` value from a list of `Value`s
        pub fn new(elements: Vec<Value>) -> Arc<Self> {
            Arc::new(List { elements: elements })
        }
    }


    /// A tuple of potentially differently typed values.
    #[derive(Debug,Clone,PartialEq,PartialOrd,Hash)]
    pub struct Tuple {
        elements: Vec<Value>,
    }

    impl fmt::Display for Tuple {
        fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
            write!(fmt, "(")?;
            let mut first = true;
            for elem in self.elements.iter() {
                if !first {
                    write!(fmt, ", ")?;
                    first = false;
                }
                write!(fmt, "{}", elem)?;
            }
            write!(fmt, ")")
        }
    }


    impl ValueT for Tuple {
        fn type_(&self) -> Type {
            types::Tuple::new(self.elements
                .iter()
                .map(|val| val.type_())
                .collect())
        }
        fn get(&self) -> &Any {
            &self.elements as &Any
        }
        fn to_sql(&self) -> Vec<&ToSql> {
            self.elements.iter().flat_map(|val| val.to_sql()).collect()
        }
        valuet_boiler!();
    }

    impl Tuple {
        /// Create a dynamic `Tuple` value from a vector of its components.
        pub fn new(elements: Vec<Value>) -> Arc<Self> {
            Arc::new(Tuple { elements: elements })
        }
    }

    /// Holds a boolean
    #[derive(Debug,PartialEq,PartialOrd,Hash)]
    pub struct Bool {
        val: bool,
    }


    impl Bool {
        /// Creates a new boolean Holmes value
        pub fn new(b: bool) -> Arc<Self> {
            Arc::new(Bool { val: b })
        }
    }

    impl fmt::Display for Bool {
        fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
            write!(fmt, "{}", self.val)
        }
    }

    impl ValueT for Bool {
        fn type_(&self) -> Type {
            Arc::new(types::Bool)
        }
        fn get(&self) -> &Any {
            &self.val as &Any
        }
        fn to_sql(&self) -> Vec<&ToSql> {
            vec![&self.val]
        }
        valuet_boiler!();
    }


    impl ToValue for bool {
        fn to_value(self) -> Value {
            Bool::new(self)
        }
    }

    impl ToValue for u64 {
        fn to_value(self) -> Value {
            UInt64::new(self)
        }
    }
    impl ToValue for ::std::string::String {
        fn to_value(self) -> Value {
            String::new(self)
        }
    }
    impl ToValue for Vec<u8> {
        fn to_value(self) -> Value {
            Bytes::new(self)
        }
    }
    /// Wrapper newtype pattern for buffers which are too large to be reasonably
    /// indexed or matched on.
    pub struct LargeBWrap {
        /// Wrapped value
        pub inner: Vec<u8>,
    }
    impl ToValue for LargeBWrap {
        fn to_value(self) -> Value {
            LargeBytes::new(self.inner)
        }
    }

    impl<T: ToValue> ToValue for Vec<T> {
        fn to_value(self) -> Value {
            List::new(self.into_iter().map(|x| x.to_value()).collect())
        }
    }
    macro_rules! to_value_tuple {
    ($($slot:ident),*) => {
      #[allow(non_snake_case)]
      impl <$($slot : ToValue),*> ToValue for ($($slot),*) {
        fn to_value(self) -> Value {
          let ($($slot),*) = self;
          Tuple::new(vec![$($slot.to_value()),*])
        }
      }
    };
  }
    to_value_tuple!(A, B);
    to_value_tuple!(A, B, C);
    to_value_tuple!(A, B, C, D);
    to_value_tuple!(A, B, C, D, E);
    to_value_tuple!(A, B, C, D, E, F);
    to_value_tuple!(A, B, C, D, E, F, G);
    to_value_tuple!(A, B, C, D, E, F, G, H);
    impl ToValue for &'static str {
        fn to_value(self) -> Value {
            String::new(self.to_string())
        }
    }

    /// Holds an unsigned 64-bit int
    #[derive(Debug,PartialEq,PartialOrd,Hash)]
    pub struct UInt64 {
        val: u64,
        sql: i64,
    }

    impl fmt::Display for UInt64 {
        fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
            write!(fmt, "{}", self.val)
        }
    }

    impl ValueT for UInt64 {
        fn type_(&self) -> Type {
            Arc::new(types::UInt64)
        }
        fn get(&self) -> &Any {
            &self.val as &Any
        }
        fn to_sql(&self) -> Vec<&ToSql> {
            vec![&self.sql]
        }
        valuet_boiler!();
    }

    impl UInt64 {
        /// Creates Holmes value holding an unsigned 64-bit integer
        pub fn new(val: u64) -> Arc<Self> {
            Arc::new(UInt64 {
                val: val,
                sql: val as i64,
            })
        }
    }

    /// Holds text
    #[derive(Debug,PartialEq,PartialOrd,Hash)]
    pub struct String {
        val: ::std::string::String,
    }

    impl fmt::Display for String {
        fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
            write!(fmt, "{:?}", self.val)
        }
    }

    impl ValueT for String {
        fn type_(&self) -> Type {
            Arc::new(types::String)
        }
        fn get(&self) -> &Any {
            &self.val as &Any
        }
        fn to_sql(&self) -> Vec<&ToSql> {
            vec![&self.val as &ToSql]
        }
        valuet_boiler!();
    }

    impl String {
        /// Creates a Holmes value holding a `String`
        pub fn new(val: ::std::string::String) -> Arc<Self> {
            Arc::new(String { val: val })
        }
    }

    /// Holds raw data
    #[derive(Debug,PartialEq,PartialOrd,Hash)]
    pub struct Bytes {
        val: Vec<u8>,
    }

    impl ValueT for Bytes {
        fn type_(&self) -> Type {
            Arc::new(types::Bytes)
        }
        fn get(&self) -> &Any {
            &self.val as &Any
        }
        fn to_sql(&self) -> Vec<&ToSql> {
            vec![&self.val as &ToSql]
        }
        valuet_boiler!();
    }

    impl fmt::Display for Bytes {
        fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
            write!(fmt, "{:?}", self.val)
        }
    }

    impl Bytes {
        /// Creates a new Holmes value holding raw data.
        pub fn new(val: Vec<u8>) -> Arc<Self> {
            Arc::new(Bytes { val: val })
        }
    }

    /// Holds large raw data
    #[derive(Debug,PartialEq,PartialOrd,Hash)]
    pub struct LargeBytes {
        val: Vec<u8>,
    }

    impl ValueT for LargeBytes {
        fn type_(&self) -> Type {
            Arc::new(types::LargeBytes)
        }
        fn get(&self) -> &Any {
            &self.val as &Any
        }
        fn to_sql(&self) -> Vec<&ToSql> {
            vec![&self.val as &ToSql]
        }
        valuet_boiler!();
    }

    impl fmt::Display for LargeBytes {
        fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
            write!(fmt, "(large)")
        }
    }

    impl LargeBytes {
        /// Creates a new Holmes value holding raw data.
        pub fn new(val: Vec<u8>) -> Arc<Self> {
            Arc::new(LargeBytes { val: val })
        }
    }

}