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
// This file was generated by gir (https://github.com/gtk-rs/gir)
// from gir-files (https://github.com/gtk-rs/gir-files)
// DO NOT EDIT

use crate::translate::*;
use crate::ChecksumType;

crate::wrapper! {
    /// An opaque structure representing a checksumming operation.
    /// To create a new GChecksum, use [`new()`][Self::new()]. To free
    /// a GChecksum, use `g_checksum_free()`.
    #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
    pub struct Checksum(Boxed<ffi::GChecksum>);

    match fn {
        copy => |ptr| ffi::g_checksum_copy(ptr),
        free => |ptr| ffi::g_checksum_free(ptr),
        type_ => || ffi::g_checksum_get_type(),
    }
}

impl Checksum {
    /// Creates a new [`Checksum`][crate::Checksum], using the checksum algorithm `checksum_type`.
    /// If the `checksum_type` is not known, [`None`] is returned.
    /// A [`Checksum`][crate::Checksum] can be used to compute the checksum, or digest, of an
    /// arbitrary binary blob, using different hashing algorithms.
    ///
    /// A [`Checksum`][crate::Checksum] works by feeding a binary blob through [`update()`][Self::update()]
    /// until there is data to be checked; the digest can then be extracted
    /// using [`string()`][Self::string()], which will return the checksum as a
    /// hexadecimal string; or [`digest()`][Self::digest()], which will return a
    /// vector of raw bytes. Once either [`string()`][Self::string()] or
    /// [`digest()`][Self::digest()] have been called on a [`Checksum`][crate::Checksum], the checksum
    /// will be closed and it won't be possible to call [`update()`][Self::update()]
    /// on it anymore.
    /// ## `checksum_type`
    /// the desired type of checksum
    ///
    /// # Returns
    ///
    /// the newly created [`Checksum`][crate::Checksum], or [`None`].
    ///  Use `g_checksum_free()` to free the memory allocated by it.
    #[doc(alias = "g_checksum_new")]
    pub fn new(checksum_type: ChecksumType) -> Option<Checksum> {
        unsafe { from_glib_full(ffi::g_checksum_new(checksum_type.into_glib())) }
    }

    /// Resets the state of the `self` back to its initial state.
    #[doc(alias = "g_checksum_reset")]
    pub fn reset(&mut self) {
        unsafe {
            ffi::g_checksum_reset(self.to_glib_none_mut().0);
        }
    }

    /// Feeds `data` into an existing [`Checksum`][crate::Checksum]. The checksum must still be
    /// open, that is [`string()`][Self::string()] or [`digest()`][Self::digest()] must
    /// not have been called on `self`.
    /// ## `data`
    /// buffer used to compute the checksum
    #[doc(alias = "g_checksum_update")]
    pub fn update(&mut self, data: &[u8]) {
        let length = data.len() as isize;
        unsafe {
            ffi::g_checksum_update(self.to_glib_none_mut().0, data.to_glib_none().0, length);
        }
    }

    /// Gets the length in bytes of digests of type `checksum_type`
    /// ## `checksum_type`
    /// a [`ChecksumType`][crate::ChecksumType]
    ///
    /// # Returns
    ///
    /// the checksum length, or -1 if `checksum_type` is
    /// not supported.
    #[doc(alias = "g_checksum_type_get_length")]
    pub fn type_get_length(checksum_type: ChecksumType) -> isize {
        unsafe { ffi::g_checksum_type_get_length(checksum_type.into_glib()) }
    }
}

unsafe impl Send for Checksum {}
unsafe impl Sync for Checksum {}