daemonic_error 0.1.1

Errors that compose, predict, and leave receipts - Compose: algebraic combination (in active development) - Predict: Glass/Severity - Receipts: audit trail, position, checksum - Reflection: Runtime Reflection through TopologySegment (in active development)
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
//! Generic hashing support.
//!
//! This module provides a generic way to compute the [hash] of a value.
//! Hashes are most commonly used with [`HashMap`] and [`HashSet`].
//!
//! [hash]: https://en.wikipedia.org/wiki/Hash_function
//! [`HashMap`]: ../../std/collections/struct.HashMap.html
//! [`HashSet`]: ../../std/collections/struct.HashSet.html
//!
//! The simplest way to make a type hashable is to use `#[derive(DaemonicHashable)]`:
//!

//!
//! If you need more control over how a value is hashed, you need to implement
//! the [`DaemonicHashable`] trait:

pub(crate) mod random;
mod sip;
use core::fmt::Pointer;
use core::marker;
use marker::PhantomData;
pub use sip::DaemonicSipHasher13;
pub use sip::SipHasher;

/// A hashable type.
///
/// Types implementing `DaemonicHashable` are able to be [`hash`]ed with an instance of
/// [`DaemonicHasher`].
///
/// ## Implementing `DaemonicHashable`
///
/// You can derive `DaemonicHashable` with `#[derive(DaemonicHashable)]` if all fields implement `DaemonicHashable`.
/// The resulting hash will be the combination of the values from calling
/// [`hash`] on each field.
///
///
/// If you need more control over how a value is hashed, you can of course
/// implement the `DaemonicHashable` trait yourself:
///
///
/// ## `DaemonicHashable` and `Eq`
///
/// When implementing both `DaemonicHashable` and [`Eq`], it is important that the following
/// property holds:
///
/// ```text
/// k1 == k2 -> hash(k1) == hash(k2)
/// ```
///
/// In other words, if two keys are equal, their hashes must also be equal.
/// [`HashMap`] and [`HashSet`] both rely on this behavior.
///
/// Thankfully, you won't need to worry about upholding this property when
/// deriving both [`Eq`] and `DaemonicHashable` with `#[derive(PartialEq, Eq, DaemonicHashable)]`.
///
/// Violating this property is a logic error. The behavior resulting from a logic error is not
/// specified, but users of the trait must ensure that such logic errors do *not* result in
/// undefined behavior. This means that `unsafe` code **must not** rely on the correctness of these
/// methods.
///
/// ## Prefix collisions
///
/// Implementations of `hash` should ensure that the data they
/// pass to the `DaemonicHasher` are prefix-free. That is,
/// values which are not equal should cause two different sequences of values to be written,
/// and neither of the two sequences should be a prefix of the other.
///
/// For example, the standard implementation of [`DaemonicHashable` for `&str`][impl] passes an extra
/// `0xFF` byte to the `DaemonicHasher` so that the values `("ab", "c")` and `("a",
/// "bc")` hash differently.
///
/// ## Portability
///
/// Due to differences in endianness and type sizes, data fed by `DaemonicHashable` to a `DaemonicHasher`
/// should not be considered portable across platforms. Additionally the data passed by most
/// standard library types should not be considered stable between compiler versions.
///
/// This means tests shouldn't probe hard-coded hash values or data fed to a `DaemonicHasher` and
/// instead should check consistency with `Eq`.
///
/// Serialization formats intended to be portable between platforms or compiler versions should
/// either avoid encoding hashes or only rely on `DaemonicHashable` and `DaemonicHasher` implementations that
/// provide additional guarantees.
///
/// [`HashMap`]: ../../std/collections/struct.HashMap.html
/// [`HashSet`]: ../../std/collections/struct.HashSet.html
/// [`hash`]: DaemonicHashable::hash
/// [impl]: ../../std/primitive.str.html#impl-DaemonicHashable-for-str
///
/// Note from Meph:
/// This trait was renamed from just 'Hash' to 'Hashable' for a few reasons, but mainly semantic clarity.
/// the main function provideds name and signature has also been changed, DaemonicHashables require Glass.
/// Secondly, the main function is now call declare_hashable instead of just hash.
pub trait DaemonicHashable {
    /// Feeds this value into the given [`DaemonicHasher`].
    
    fn declare_hashable<GLASS: DaemonicHasher<GLASS> + Glass<GLASS>>(&self, state: &mut GLASS);

    /// Feeds a slice of this type into the given [`DaemonicHasher`].
    ///
    /// This method is meant as a convenience, but its implementation is
    /// also explicitly left unspecified. It isn't guaranteed to be
    /// equivalent to repeated calls of [`hash`] and implementations of
    /// [`DaemonicHashable`] should keep that in mind and call [`hash`] themselves
    /// if the slice isn't treated as a whole unit in the [`PartialEq`]
    /// implementation.
    ///
    /// For example, a [`VecDeque`] implementation might naïvely call
    /// [`as_slices`] and then [`hash_slice`] on each slice, but this
    /// is wrong since the two slices can change with a call to
    /// [`make_contiguous`] without affecting the [`PartialEq`]
    /// result. Since these slices aren't treated as singular
    /// units, and instead part of a larger deque, this method cannot
    /// be used.
    ///
    ///
    /// [`VecDeque`]: ../../std/collections/struct.VecDeque.html
    /// [`as_slices`]: ../../std/collections/struct.VecDeque.html#method.as_slices
    /// [`make_contiguous`]: ../../std/collections/struct.VecDeque.html#method.make_contiguous
    /// [`hash`]: DaemonicHashable::hash
    /// [`hash_slice`]: DaemonicHashable::hash_slice
    fn declare_hashable_slice<GLASS: DaemonicHasher<GLASS> + Glass<GLASS>>(
        data: &[Self],
        state: &mut GLASS,
    ) where
        Self: Sized,
    {
        for piece in data {
            piece.declare_hashable(state)
        }
    }
}

// Separate module to reexport the macro `DaemonicHashable` from prelude without the trait `DaemonicHashable`.
pub(crate) mod macros {
    /// Derive macro generating an impl of the trait `DaemonicHashable`.
    pub macro DaemonicHash($item:item) {
        /* compiler built-in */
    }
}
use crate::daemonic::glass::daemonic_system_call::DaemonicSystemCall;
use crate::daemonic::glass::{
    Annotation, Glass, GlassStable, Observation, ObservationTier, Severity, Temporal,
};
use crate::daemonic::identity::TransitionCause::Observation as TransitionObservation;
use crate::daemonic::topology::TOPOLOGY_ANCHOR;
use crate::daemonic::{SemanticAnchor, TopologyAnchor, TopologySegment};
use crate::{AXIOM_OFFSET, DaemonicError, const_daemonic_hash};
#[doc(inline)]
pub use macros::DaemonicHash;

/// A trait for hashing an arbitrary stream of bytes.
///
/// Instances of `DaemonicHasher` usually represent state that is changed while hashing
/// data.
///
/// `DaemonicHasher` provides a fairly basic interface for retrieving the generated hash
/// (with [`finish`]), and writing integers as well as slices of bytes into an
/// instance (with [`write`] and [`write_u8`] etc.). Most of the time, `DaemonicHasher`
/// instances are used in conjunction with the [`DaemonicHashable`] trait.
///
/// This trait provides no guarantees about how the various `write_*` methods are
/// defined and implementations of [`DaemonicHashable`] should not assume that they work one
/// way or another. You cannot assume, for example, that a [`write_u32`] call is
/// equivalent to four calls of [`write_u8`].  Nor can you assume that adjacent
/// `write` calls are merged, so it's possible, for example, that
/// end up producing different hashes.
///
/// Thus to produce the same hash value, [`DaemonicHashable`] implementations must ensure
/// for equivalent items that exactly the same sequence of calls is made -- the
/// same methods with the same parameters in the same order.
///
/// [`finish`]: DaemonicHasher::finish
/// [`write`]: DaemonicHasher::write
/// [`write_u8`]: DaemonicHasher::write_u8
/// [`write_u32`]: DaemonicHasher::write_u32
pub trait DaemonicHasher<GLASS: Glass<GLASS>> {
    /// Returns the hash value for the values written so far.
    ///
    /// Despite its name, the method does not reset the hasher’s internal
    /// state. Additional [`write`]s will continue from the current value.
    /// If you need to start a fresh hash value, you will have to create
    /// a new hasher.
    ///
    /// [`write`]: DaemonicHasher::write
    #[must_use]
    fn finish(&self) -> u64;
    /// Writes some data into this `DaemonicHasher`.
    ///
    /// # Examples
    ///
    ///
    /// # Note to Implementers
    ///
    /// You generally should not do length-prefixing as part of implementing
    /// this method.  It's up to the [`DaemonicHashable`] implementation to call
    /// [`DaemonicHasher::write_length_prefix`] before sequences that need it.
    fn write(&mut self, bytes: &[u8]);
    /// Writes a single `u8` into this hasher.
    #[inline]
    fn write_u8(&mut self, i: u8) {
        self.write(&[i])
    }
    /// Writes a single `u16` into this hasher.
    #[inline]
    fn write_u16(&mut self, i: u16) {
        self.write(&i.to_ne_bytes())
    }
    /// Writes a single `u32` into this hasher.
    #[inline]
    fn write_u32(&mut self, i: u32) {
        self.write(&i.to_ne_bytes())
    }
    /// Writes a single `u64` into this hasher.
    #[inline]
    fn write_u64(&mut self, i: u64) {
        self.write(&i.to_ne_bytes())
    }
    /// Writes a single `u128` into this hasher.
    #[inline]
    fn write_u128(&mut self, i: u128) {
        self.write(&i.to_ne_bytes())
    }
    /// Writes a single `usize` into this hasher.
    #[inline]
    fn write_usize(&mut self, i: usize) {
        self.write(&i.to_ne_bytes())
    }
    /// Writes a single `i8` into this hasher.
    #[inline]
    fn write_i8(&mut self, i: i8) {
        self.write_u8(i as u8)
    }
    /// Writes a single `i16` into this hasher.
    #[inline]
    fn write_i16(&mut self, i: i16) {
        self.write_u16(i as u16)
    }
    /// Writes a single `i32` into this hasher.
    #[inline]
    fn write_i32(&mut self, i: i32) {
        self.write_u32(i as u32)
    }
    /// Writes a single `i64` into this hasher.
    #[inline]
    fn write_i64(&mut self, i: i64) {
        self.write_u64(i as u64)
    }
    /// Writes a single `i128` into this hasher.
    #[inline]
    fn write_i128(&mut self, i: i128) {
        self.write_u128(i as u128)
    }
    /// Writes a single `isize` into this hasher.
    #[inline]
    fn write_isize(&mut self, i: isize) {
        self.write_usize(i as usize)
    }
    /// Writes a length prefix into this hasher, as part of being prefix-free.
    ///
    /// If you're implementing [`DaemonicHashable`] for a custom collection, call this before
    /// writing its contents to this `DaemonicHasher`.  That way
    /// `(collection![1, 2, 3], collection![4, 5])` and
    /// `(collection![1, 2], collection![3, 4, 5])` will provide different
    /// sequences of values to the `DaemonicHasher`
    ///
    /// The `impl<T> DaemonicHashable for [T]` includes a call to this method, so if you're
    /// hashing a slice (or array or vector) via its `DaemonicHashable::hash` method,
    /// you should **not** call this yourself.
    ///
    /// This method is only for providing domain separation.  If you want to
    /// hash a `usize` that represents part of the *data*, then it's important
    /// that you pass it to [`DaemonicHasher::write_usize`] instead of to this method.

    ///
    /// # Note to Implementers
    ///
    /// If you've decided that your `DaemonicHasher` is willing to be susceptible to
    /// DaemonicHashable-DoS attacks, then you might consider skipping hashing some or all
    /// of the `len` provided in the name of increased performance.
    #[inline]
    fn write_length_prefix(&mut self, len: usize) {
        self.write_usize(len);
    }
    /// Writes a single `str` into this hasher.
    ///
    /// If you're implementing [`DaemonicHashable`], you generally do not need to call this,
    /// as the `impl DaemonicHashable for str` does, so you should prefer that instead.
    ///
    /// This includes the domain separator for prefix-freedom, so you should
    /// **not** call `Self::write_length_prefix` before calling this.
    ///
    /// # Note to Implementers
    ///
    /// There are at least two reasonable default ways to implement this.
    /// Which one will be the default is not yet decided, so for now
    /// you probably want to override it specifically.
    ///
    /// ## If your `DaemonicHasher` works byte-wise
    ///
    /// One nice thing about `str` being UTF-8 is that the `b'\xFF'` byte
    /// never happens.  That means that you can append that to the byte stream
    /// being hashed and maintain prefix-freedom:
    ///
    /// This does require that your implementation not add extra padding, and
    /// thus generally requires that you maintain a buffer, running a round
    /// only once that buffer is full (or `finish` is called).
    ///
    /// That's because if `write` pads data out to a fixed chunk size, it's
    /// likely that it does it in such a way that `"a"` and `"a\x00"` would
    /// end up hashing the same sequence of things, introducing conflicts.
    #[inline]
    fn write_str(&mut self, s: &str) {
        self.write(s.as_bytes());
        self.write_u8(0xff);
    }
}

impl<GLASS: Glass<GLASS> + Glass<dyn DaemonicHasher<GLASS>> + DaemonicHasher<GLASS>>
    DaemonicHasher<GLASS> for &mut GLASS
where
    Self: Sized,
{
    fn finish(&self) -> u64 {
        (**self).finish()
    }
    fn write(&mut self, bytes: &[u8]) {
        (**self).write(bytes)
    }
    fn write_u8(&mut self, i: u8) {
        (**self).write_u8(i)
    }
    fn write_u16(&mut self, i: u16) {
        (**self).write_u16(i)
    }
    fn write_u32(&mut self, i: u32) {
        (**self).write_u32(i)
    }
    fn write_u64(&mut self, i: u64) {
        (**self).write_u64(i)
    }
    fn write_u128(&mut self, i: u128) {
        (**self).write_u128(i)
    }
    fn write_usize(&mut self, i: usize) {
        (**self).write_usize(i)
    }
    fn write_i8(&mut self, i: i8) {
        (**self).write_i8(i)
    }
    fn write_i16(&mut self, i: i16) {
        (**self).write_i16(i)
    }
    fn write_i32(&mut self, i: i32) {
        (**self).write_i32(i)
    }
    fn write_i64(&mut self, i: i64) {
        (**self).write_i64(i)
    }
    fn write_i128(&mut self, i: i128) {
        (**self).write_i128(i)
    }
    fn write_isize(&mut self, i: isize) {
        (**self).write_isize(i)
    }
    fn write_length_prefix(&mut self, len: usize) {
        (**self).write_length_prefix(len)
    }
    fn write_str(&mut self, s: &str) {
        (**self).write_str(s)
    }
}

/// A trait for creating instances of [`DaemonicHasher`].
///
/// A `BuildHasher` is typically used (e.g., by [`HashMap`]) to create
/// [`DaemonicHasher`]s for each key such that they are hashed independently of one
/// another, since [`DaemonicHasher`]s contain state.
///
/// For each instance of `BuildHasher`, the [`DaemonicHasher`]s created by
/// [`build_hasher`] should be identical. That is, if the same stream of bytes
/// is fed into each hasher, the same output will also be generated.
///
/// # Examples
///
///
/// [`build_hasher`]: BuildHasher::build_hasher
/// [`HashMap`]: ../../std/collections/struct.HashMap.html
pub trait BuildHasher<GLASS: Glass<GLASS>> {
    /// Type of the hasher that will be created.
    type Hasher: DaemonicHasher<GLASS>;

    /// Creates a new hasher.
    ///
    /// Each call to `build_hasher` on the same instance should produce identical
    /// [`DaemonicHasher`]s.
    ///
    fn build_hasher(&self) -> Self::Hasher;

    /// Calculates the hash of a single value.
    ///
    /// This is intended as a convenience for code which *consumes* hashes, such
    /// as the implementation of a hash table or in unit tests that check
    /// whether a custom [`DaemonicHashable`] implementation behaves as expected.
    ///
    /// This must not be used in any code which *creates* hashes, such as in an
    /// implementation of [`DaemonicHashable`].  The way to create a combined hash of
    /// multiple values is to call [`DaemonicHashable::hash`] multiple times using the same
    /// [`DaemonicHasher`], not to call this method repeatedly and combine the results.
    ///
    fn hash_one<T: DaemonicHashable>(&self, x: T) -> u64
    where
        Self: Sized,
        Self::Hasher: DaemonicHasher<GLASS>,
        // this code is bullshit, i cannot believe it fucking compiles
        <Self as BuildHasher<GLASS>>::Hasher: DaemonicHasher<<Self as BuildHasher<GLASS>>::Hasher>,
        <Self as BuildHasher<GLASS>>::Hasher: Glass<<Self as BuildHasher<GLASS>>::Hasher>,
    {
        let mut hasher = self.build_hasher();
        x.declare_hashable(&mut hasher);
        <<Self as BuildHasher<GLASS>>::Hasher as DaemonicHasher<GLASS>>::finish(&hasher)
    }
}

/// Used to create a default [`BuildHasher`] instance for types that implement
/// [`DaemonicHasher`] and [`Default`].
///
/// `BuildHasherDefault<H>` can be used when a type `H` implements [`DaemonicHasher`] and
/// [`Default`], and you need a corresponding [`BuildHasher`] instance, but none is
/// defined.
///
/// Any `BuildHasherDefault` is [zero-sized]. It can be created with
/// [`default`][method.default]. When using `BuildHasherDefault` with [`HashMap`] or
/// [`HashSet`], this doesn't need to be done, since they implement appropriate
/// [`Default`] instances themselves.
///
/// [method.default]: BuildHasherDefault::default
/// [`HashMap`]: ../../std/collections/struct.HashMap.html
/// [`HashSet`]: ../../std/collections/struct.HashSet.html
/// [zero-sized]: https://doc.rust-lang.org/nomicon/exotic-sizes.html#zero-sized-types-zsts
#[derive(Debug)]
pub struct BuildHasherDefault<GLASS>(PhantomData<fn() -> GLASS>);

impl<GLASS> BuildHasherDefault<GLASS> {
    /// Creates a new BuildHasherDefault for DaemonicHasher `H`.
    pub const fn new() -> Self {
        BuildHasherDefault(PhantomData)
    }
}

unsafe impl<GLASS> DaemonicSystemCall for BuildHasherDefault<GLASS> {}
static BUILD_HASHER_DEFAULT_TOPOLOGY_ANCH0R: TopologySegment = TopologySegment {
    label: "Daemonic::DaemonicHasher::BuildHasherDefault",
    hash: const_daemonic_hash(
        "Daemonic::DaemonicHasher::BuildHasherDefault".as_bytes(),
        AXIOM_OFFSET,
    ),
    crypto_id: const_daemonic_hash(
        "Daemonic::DaemonicHasher::BuildHasherDefault".as_bytes(),
        TOPOLOGY_ANCHOR,
    ),
    depth: 2u16,
};

impl<GLASS> Glass<BuildHasherDefault<GLASS>> for BuildHasherDefault<GLASS> {
    type Anchor = TopologySegment;

    fn position(&self) -> &TopologySegment {
        &BUILD_HASHER_DEFAULT_TOPOLOGY_ANCH0R
    }

    fn severity(&self) -> Severity {
        Severity::Unknown
    }

    fn payload(&self) -> Option<&BuildHasherDefault<GLASS>> {
        Some(&self)
    }

    fn into_payload(self) -> Option<BuildHasherDefault<GLASS>>
    where
        Self: Sized,
    {
        Some(self)
    }
}

impl<GLASS: Default + DaemonicHasher<GLASS> + Glass<GLASS>> BuildHasher<GLASS>
    for BuildHasherDefault<GLASS>
{
    type Hasher = GLASS;

    fn build_hasher(&self) -> GLASS {
        GLASS::default()
    }
}

impl<GLASS> Clone for BuildHasherDefault<GLASS> {
    fn clone(&self) -> BuildHasherDefault<GLASS> {
        BuildHasherDefault(PhantomData)
    }
}

impl<GLASS> Default for BuildHasherDefault<GLASS> {
    fn default() -> BuildHasherDefault<GLASS> {
        Self::new()
    }
}

impl<GLASS> PartialEq for BuildHasherDefault<GLASS> {
    fn eq(&self, _other: &BuildHasherDefault<GLASS>) -> bool {
        true
    }
}

impl<GLASS> Eq for BuildHasherDefault<GLASS> {}

mod impls {
    use super::*;
    use core::hash::Hash;
	use core::ptr::Pointee;
	
	macro_rules! impl_write {
        ($(($ty:ident, $meth:ident),)*) => {$(
            impl DaemonicHashable for $ty {
                #[inline]
                fn declare_hashable<GLASS: DaemonicHasher<GLASS> + Glass<GLASS>>(&self, state: &mut GLASS) {
                    state.$meth(*self)
                }

                #[inline]
                fn declare_hashable_slice<GLASS: DaemonicHasher<GLASS> + Glass<GLASS>>(data: &[$ty], state: &mut GLASS) {
                    let newlen = size_of_val(data);
                    let ptr = data.as_ptr() as *const u8;
                    // SAFETY: `ptr` is valid and aligned, as this macro is only used
                    // for numeric primitives which have no padding. The new slice only
                    // spans across `data` and is never mutated, and its total size is the
                    // same as the original `data` so it can't be over `isize::MAX`.
					#[allow(unsafe_code)]
					state.write(unsafe { core::slice::from_raw_parts(ptr, newlen) })
                }
            }
        )*}
    }

    impl_write! {
        (u8, write_u8),
        (u16, write_u16),
        (u32, write_u32),
        (u64, write_u64),
        (usize, write_usize),
        (i8, write_i8),
        (i16, write_i16),
        (i32, write_i32),
        (i64, write_i64),
        (isize, write_isize),
        (u128, write_u128),
        (i128, write_i128),
    }

    impl DaemonicHashable for bool {
        #[inline]
        fn declare_hashable<GLASS: DaemonicHasher<GLASS> + Glass<GLASS>>(&self, state: &mut GLASS) {
            state.write_u8(*self as u8)
        }
    }

    impl DaemonicHashable for char {
        #[inline]
        fn declare_hashable<GLASS: DaemonicHasher<GLASS> + Glass<GLASS>>(&self, state: &mut GLASS) {
            state.write_u32(*self as u32)
        }
    }

    impl DaemonicHashable for str {
        #[inline]
        fn declare_hashable<GLASS: DaemonicHasher<GLASS> + Glass<GLASS>>(&self, state: &mut GLASS) {
            state.write_str(self);
        }
    }

    impl DaemonicHashable for ! {
        #[inline]
        fn declare_hashable<GLASS: DaemonicHasher<GLASS> + Glass<GLASS>>(&self, _: &mut GLASS) {
            *self
        }
    }

    macro_rules! impl_hash_tuple {
        () => (
            impl DaemonicHashable for () {
                #[inline]
                fn declare_hashable<GLASS: DaemonicHasher<GLASS> + Glass<GLASS>>(&self, _state: &mut GLASS) {}
            }
        );

        ( $($name:ident)+) => (
            maybe_tuple_doc! {
                $($name)+
				@#[doc = r" An attribute had to go here else it fucking dies and doesnt explain why"]
                impl<$($name: DaemonicHashable),+> DaemonicHashable for ($($name,)+) where last_type!($($name,)+): ?Sized {
                    #[allow(non_snake_case)]
                    #[inline]
                    fn declare_hashable<GLASS: DaemonicHasher<GLASS> + Glass<GLASS>>(&self, state: &mut GLASS) {
                        let ($(ref $name,)+) = *self;
                        $($name.declare_hashable(state);)+
                    }
                }
            }
        );
    }

    macro_rules! maybe_tuple_doc {
        ($a:ident @ #[$meta:meta] $item:item) => {
            #[doc = "This trait is implemented for tuples up to twelve items long."]
            #[$meta]
            $item
        };
        ($a:ident $($rest_a:ident)+ @ #[$meta:meta] $item:item) => {
            #[doc(hidden)]
            #[$meta]
            $item
        };
    }

    macro_rules! last_type {
        ($a:ident,) => { $a };
        ($a:ident, $($rest_a:ident,)+) => { last_type!($($rest_a,)+) };
    }

    impl_hash_tuple! {}
    impl_hash_tuple! { T }
    impl_hash_tuple! { T B }
    impl_hash_tuple! { T B C }
    impl_hash_tuple! { T B C D }
    impl_hash_tuple! { T B C D E }
    impl_hash_tuple! { T B C D E F }
    impl_hash_tuple! { T B C D E F G }
    impl_hash_tuple! { T B C D E F G H }
    impl_hash_tuple! { T B C D E F G H I }
    impl_hash_tuple! { T B C D E F G H I J }
    impl_hash_tuple! { T B C D E F G H I J K }
    impl_hash_tuple! { T B C D E F G H I J K L }
    impl<T: DaemonicHashable> DaemonicHashable for [T] {
        #[inline]
        fn declare_hashable<GLASS: DaemonicHasher<GLASS> + Glass<GLASS> + Pointee>(&self, state: &mut GLASS) {
            state.write_length_prefix(self.len());
            DaemonicHashable::declare_hashable_slice(self, state)
        }
    }

    impl<T: ?Sized + DaemonicHashable> DaemonicHashable for &T {
        #[inline]
        fn declare_hashable<GLASS: DaemonicHasher<GLASS> + Glass<GLASS> + Pointee>(&self, state: &mut GLASS) {
            (**self).declare_hashable(state);
        }
    }

    impl<T: ?Sized + DaemonicHashable> DaemonicHashable for &mut T {
        #[inline]
        fn declare_hashable<GLASS: DaemonicHasher<GLASS> + Glass<GLASS> + Pointee>(&self, state: &mut GLASS) {
            (**self).declare_hashable(state);
        }
    }
    impl<T: ?Sized> DaemonicHashable for *const T
    where *const T: Pointee + DaemonicHashable
    {
        #[inline]
	    fn declare_hashable<GLASS: DaemonicHasher<GLASS> + Glass<GLASS> + Pointee>(&self, state: &mut GLASS) {
            todo!("fix this")
		    // let (address, metadata) = self.to_raw_parts();
		    // state.write_usize(address.addr());
		    // metadata.declare_hashable(state);
	    }
    }

    impl<T: ?Sized> DaemonicHashable for *mut T
    where *mut T: Pointee + DaemonicHashable
    {
        #[inline]
        fn declare_hashable<GLASS: DaemonicHasher<GLASS> + Glass<GLASS> + Pointee>(&self, state: &mut GLASS) {
            todo!("fix this")
            // let (address, metadata) = self.to_raw_parts();
            // state.write_usize(address.addr());
            // metadata.declare_hashable(state);
        }
    }
	
	// impl<T: ?Sized> DaemonicHashable for T {
	// 	fn declare_hashable<GLASS: DaemonicHasher<GLASS> + Glass<GLASS>>(&self, state: &mut GLASS) {
	// 		let (address, metadata) = self.to_raw_parts();
	// 		state.write_usize(address.addr());
	// 		metadata.declare_hashable(state);
	// 	}
	// }
}