pub struct Context<H: DigestAlg>(_);
Expand description

A Digest Context for the H digest algorithm

Implementations§

Create a new digest context

Examples found in repository?
src/chain_crypto/digest.rs (line 284)
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
    pub fn digest(slice: &[u8]) -> Self {
        let mut ctx = Context::new();
        ctx.append_data(slice);
        ctx.finalize()
    }
}

use std::marker::PhantomData;

/// A typed version of Digest
pub struct DigestOf<H: DigestAlg, T> {
    inner: Digest<H>,
    marker: PhantomData<T>,
}

unsafe impl<H: DigestAlg, T> Send for DigestOf<H, T> {}

impl<H: DigestAlg, T> Clone for DigestOf<H, T> {
    fn clone(&self) -> Self {
        DigestOf {
            inner: self.inner.clone(),
            marker: self.marker.clone(),
        }
    }
}

impl<H: DigestAlg, T> From<DigestOf<H, T>> for Digest<H> {
    fn from(d: DigestOf<H, T>) -> Self {
        d.inner
    }
}

impl<H: DigestAlg, T> From<Digest<H>> for DigestOf<H, T> {
    fn from(d: Digest<H>) -> Self {
        DigestOf {
            inner: d,
            marker: PhantomData,
        }
    }
}

impl<H: DigestAlg, T> PartialEq for DigestOf<H, T> {
    fn eq(&self, other: &Self) -> bool {
        &self.inner == &other.inner
    }
}

impl<H: DigestAlg, T> Eq for DigestOf<H, T> {}

impl<H: DigestAlg, T> PartialOrd for DigestOf<H, T> {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        self.inner.partial_cmp(&other.inner)
    }
}

impl<H: DigestAlg, T> Ord for DigestOf<H, T> {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.inner.cmp(&other.inner)
    }
}

impl<H: DigestAlg, T> AsRef<[u8]> for DigestOf<H, T> {
    fn as_ref(&self) -> &[u8] {
        self.inner.as_ref()
    }
}

impl<H: DigestAlg, T> Hash for DigestOf<H, T> {
    fn hash<HA: Hasher>(&self, state: &mut HA) {
        self.inner.hash(state)
    }
}

impl<H: DigestAlg, T> TryFrom<&[u8]> for DigestOf<H, T> {
    type Error = Error;
    fn try_from(slice: &[u8]) -> Result<Self, Self::Error> {
        Digest::<H>::try_from(slice).map(|d| d.into())
    }
}

impl<H: DigestAlg, T> FromStr for DigestOf<H, T> {
    type Err = Error;
    fn from_str(s: &str) -> result::Result<Self, Self::Err> {
        Digest::<H>::from_str(s).map(|d| d.into())
    }
}

impl<H: DigestAlg, T> fmt::Display for DigestOf<H, T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.inner.fmt(f)
    }
}
impl<H: DigestAlg, T> fmt::Debug for DigestOf<H, T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.inner.fmt(f)
    }
}

impl<H: DigestAlg, T> DigestOf<H, T> {
    /// Coerce a digest of T, to a digest of U
    pub fn coerce<U>(&self) -> DigestOf<H, U> {
        DigestOf {
            inner: self.inner.clone(),
            marker: PhantomData,
        }
    }

    pub fn digest_byteslice<'a>(byteslice: &ByteSlice<'a, T>) -> Self {
        let mut ctx = Context::new();
        ctx.append_data(byteslice.as_slice());
        DigestOf {
            inner: ctx.finalize(),
            marker: PhantomData,
        }
    }

    /// Get the digest of object T, given its AsRef<[u8]> implementation
    pub fn digest(obj: &T) -> Self
    where
        T: AsRef<[u8]>,
    {
        let mut ctx = Context::new();
        ctx.append_data(obj.as_ref());
        DigestOf {
            inner: ctx.finalize(),
            marker: PhantomData,
        }
    }

    /// Get the digest of object T, given its serialization function in closure
    pub fn digest_with<F>(obj: &T, f: F) -> Self
    where
        F: FnOnce(&T) -> &[u8],
    {
        let mut ctx = Context::new();
        ctx.append_data(f(obj));
        DigestOf {
            inner: ctx.finalize(),
            marker: PhantomData,
        }
    }

Append data in the context

Examples found in repository?
src/chain_crypto/digest.rs (line 285)
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
    pub fn digest(slice: &[u8]) -> Self {
        let mut ctx = Context::new();
        ctx.append_data(slice);
        ctx.finalize()
    }
}

use std::marker::PhantomData;

/// A typed version of Digest
pub struct DigestOf<H: DigestAlg, T> {
    inner: Digest<H>,
    marker: PhantomData<T>,
}

unsafe impl<H: DigestAlg, T> Send for DigestOf<H, T> {}

impl<H: DigestAlg, T> Clone for DigestOf<H, T> {
    fn clone(&self) -> Self {
        DigestOf {
            inner: self.inner.clone(),
            marker: self.marker.clone(),
        }
    }
}

impl<H: DigestAlg, T> From<DigestOf<H, T>> for Digest<H> {
    fn from(d: DigestOf<H, T>) -> Self {
        d.inner
    }
}

impl<H: DigestAlg, T> From<Digest<H>> for DigestOf<H, T> {
    fn from(d: Digest<H>) -> Self {
        DigestOf {
            inner: d,
            marker: PhantomData,
        }
    }
}

impl<H: DigestAlg, T> PartialEq for DigestOf<H, T> {
    fn eq(&self, other: &Self) -> bool {
        &self.inner == &other.inner
    }
}

impl<H: DigestAlg, T> Eq for DigestOf<H, T> {}

impl<H: DigestAlg, T> PartialOrd for DigestOf<H, T> {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        self.inner.partial_cmp(&other.inner)
    }
}

impl<H: DigestAlg, T> Ord for DigestOf<H, T> {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.inner.cmp(&other.inner)
    }
}

impl<H: DigestAlg, T> AsRef<[u8]> for DigestOf<H, T> {
    fn as_ref(&self) -> &[u8] {
        self.inner.as_ref()
    }
}

impl<H: DigestAlg, T> Hash for DigestOf<H, T> {
    fn hash<HA: Hasher>(&self, state: &mut HA) {
        self.inner.hash(state)
    }
}

impl<H: DigestAlg, T> TryFrom<&[u8]> for DigestOf<H, T> {
    type Error = Error;
    fn try_from(slice: &[u8]) -> Result<Self, Self::Error> {
        Digest::<H>::try_from(slice).map(|d| d.into())
    }
}

impl<H: DigestAlg, T> FromStr for DigestOf<H, T> {
    type Err = Error;
    fn from_str(s: &str) -> result::Result<Self, Self::Err> {
        Digest::<H>::from_str(s).map(|d| d.into())
    }
}

impl<H: DigestAlg, T> fmt::Display for DigestOf<H, T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.inner.fmt(f)
    }
}
impl<H: DigestAlg, T> fmt::Debug for DigestOf<H, T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.inner.fmt(f)
    }
}

impl<H: DigestAlg, T> DigestOf<H, T> {
    /// Coerce a digest of T, to a digest of U
    pub fn coerce<U>(&self) -> DigestOf<H, U> {
        DigestOf {
            inner: self.inner.clone(),
            marker: PhantomData,
        }
    }

    pub fn digest_byteslice<'a>(byteslice: &ByteSlice<'a, T>) -> Self {
        let mut ctx = Context::new();
        ctx.append_data(byteslice.as_slice());
        DigestOf {
            inner: ctx.finalize(),
            marker: PhantomData,
        }
    }

    /// Get the digest of object T, given its AsRef<[u8]> implementation
    pub fn digest(obj: &T) -> Self
    where
        T: AsRef<[u8]>,
    {
        let mut ctx = Context::new();
        ctx.append_data(obj.as_ref());
        DigestOf {
            inner: ctx.finalize(),
            marker: PhantomData,
        }
    }

    /// Get the digest of object T, given its serialization function in closure
    pub fn digest_with<F>(obj: &T, f: F) -> Self
    where
        F: FnOnce(&T) -> &[u8],
    {
        let mut ctx = Context::new();
        ctx.append_data(f(obj));
        DigestOf {
            inner: ctx.finalize(),
            marker: PhantomData,
        }
    }

Finalize a context and create a digest

Examples found in repository?
src/chain_crypto/digest.rs (line 286)
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
    pub fn digest(slice: &[u8]) -> Self {
        let mut ctx = Context::new();
        ctx.append_data(slice);
        ctx.finalize()
    }
}

use std::marker::PhantomData;

/// A typed version of Digest
pub struct DigestOf<H: DigestAlg, T> {
    inner: Digest<H>,
    marker: PhantomData<T>,
}

unsafe impl<H: DigestAlg, T> Send for DigestOf<H, T> {}

impl<H: DigestAlg, T> Clone for DigestOf<H, T> {
    fn clone(&self) -> Self {
        DigestOf {
            inner: self.inner.clone(),
            marker: self.marker.clone(),
        }
    }
}

impl<H: DigestAlg, T> From<DigestOf<H, T>> for Digest<H> {
    fn from(d: DigestOf<H, T>) -> Self {
        d.inner
    }
}

impl<H: DigestAlg, T> From<Digest<H>> for DigestOf<H, T> {
    fn from(d: Digest<H>) -> Self {
        DigestOf {
            inner: d,
            marker: PhantomData,
        }
    }
}

impl<H: DigestAlg, T> PartialEq for DigestOf<H, T> {
    fn eq(&self, other: &Self) -> bool {
        &self.inner == &other.inner
    }
}

impl<H: DigestAlg, T> Eq for DigestOf<H, T> {}

impl<H: DigestAlg, T> PartialOrd for DigestOf<H, T> {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        self.inner.partial_cmp(&other.inner)
    }
}

impl<H: DigestAlg, T> Ord for DigestOf<H, T> {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.inner.cmp(&other.inner)
    }
}

impl<H: DigestAlg, T> AsRef<[u8]> for DigestOf<H, T> {
    fn as_ref(&self) -> &[u8] {
        self.inner.as_ref()
    }
}

impl<H: DigestAlg, T> Hash for DigestOf<H, T> {
    fn hash<HA: Hasher>(&self, state: &mut HA) {
        self.inner.hash(state)
    }
}

impl<H: DigestAlg, T> TryFrom<&[u8]> for DigestOf<H, T> {
    type Error = Error;
    fn try_from(slice: &[u8]) -> Result<Self, Self::Error> {
        Digest::<H>::try_from(slice).map(|d| d.into())
    }
}

impl<H: DigestAlg, T> FromStr for DigestOf<H, T> {
    type Err = Error;
    fn from_str(s: &str) -> result::Result<Self, Self::Err> {
        Digest::<H>::from_str(s).map(|d| d.into())
    }
}

impl<H: DigestAlg, T> fmt::Display for DigestOf<H, T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.inner.fmt(f)
    }
}
impl<H: DigestAlg, T> fmt::Debug for DigestOf<H, T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.inner.fmt(f)
    }
}

impl<H: DigestAlg, T> DigestOf<H, T> {
    /// Coerce a digest of T, to a digest of U
    pub fn coerce<U>(&self) -> DigestOf<H, U> {
        DigestOf {
            inner: self.inner.clone(),
            marker: PhantomData,
        }
    }

    pub fn digest_byteslice<'a>(byteslice: &ByteSlice<'a, T>) -> Self {
        let mut ctx = Context::new();
        ctx.append_data(byteslice.as_slice());
        DigestOf {
            inner: ctx.finalize(),
            marker: PhantomData,
        }
    }

    /// Get the digest of object T, given its AsRef<[u8]> implementation
    pub fn digest(obj: &T) -> Self
    where
        T: AsRef<[u8]>,
    {
        let mut ctx = Context::new();
        ctx.append_data(obj.as_ref());
        DigestOf {
            inner: ctx.finalize(),
            marker: PhantomData,
        }
    }

    /// Get the digest of object T, given its serialization function in closure
    pub fn digest_with<F>(obj: &T, f: F) -> Self
    where
        F: FnOnce(&T) -> &[u8],
    {
        let mut ctx = Context::new();
        ctx.append_data(f(obj));
        DigestOf {
            inner: ctx.finalize(),
            marker: PhantomData,
        }
    }

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.