Skip to main content

glib/auto/
checksum.rs

1// This file was generated by gir (https://github.com/gtk-rs/gir)
2// from gir-files (https://github.com/gtk-rs/gir-files)
3// DO NOT EDIT
4
5use crate::{ChecksumType, ffi, translate::*};
6
7crate::wrapper! {
8    /// s data
9    /// available and then using [`string()`][Self::string()] or
10    /// [`digest()`][Self::digest()] to compute the checksum and return it
11    /// either as a string in hexadecimal form, or as a raw sequence of bytes. To
12    /// compute the checksum for binary blobs and nul-terminated strings in
13    /// one go, use the convenience functions [`compute_checksum_for_data()`][crate::compute_checksum_for_data()]
14    /// and `compute_checksum_for_string()`, respectively.
15    #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
16    pub struct Checksum(Boxed<ffi::GChecksum>);
17
18    match fn {
19        copy => |ptr| ffi::g_checksum_copy(ptr),
20        free => |ptr| ffi::g_checksum_free(ptr),
21        type_ => || ffi::g_checksum_get_type(),
22    }
23}
24
25impl Checksum {
26    /// Creates a new #GChecksum, using the checksum algorithm @checksum_type.
27    /// If the @checksum_type is not known, [`None`] is returned.
28    /// A #GChecksum can be used to compute the checksum, or digest, of an
29    /// arbitrary binary blob, using different hashing algorithms.
30    ///
31    /// A #GChecksum works by feeding a binary blob through g_checksum_update()
32    /// until there is data to be checked; the digest can then be extracted
33    /// using g_checksum_get_string(), which will return the checksum as a
34    /// hexadecimal string; or g_checksum_get_digest(), which will return a
35    /// vector of raw bytes. Once either g_checksum_get_string() or
36    /// g_checksum_get_digest() have been called on a #GChecksum, the checksum
37    /// will be closed and it won't be possible to call g_checksum_update()
38    /// on it anymore.
39    /// ## `checksum_type`
40    /// the desired type of checksum
41    ///
42    /// # Returns
43    ///
44    /// the newly created #GChecksum, or [`None`].
45    ///   Use g_checksum_free() to free the memory allocated by it.
46    #[doc(alias = "g_checksum_new")]
47    pub fn new(checksum_type: ChecksumType) -> Option<Checksum> {
48        unsafe { from_glib_full(ffi::g_checksum_new(checksum_type.into_glib())) }
49    }
50
51    /// Resets the state of the @self back to its initial state.
52    #[doc(alias = "g_checksum_reset")]
53    pub fn reset(&mut self) {
54        unsafe {
55            ffi::g_checksum_reset(self.to_glib_none_mut().0);
56        }
57    }
58
59    /// Feeds @data into an existing #GChecksum. The checksum must still be
60    /// open, that is g_checksum_get_string() or g_checksum_get_digest() must
61    /// not have been called on @self.
62    /// ## `data`
63    /// buffer used to compute the checksum
64    #[doc(alias = "g_checksum_update")]
65    pub fn update(&mut self, data: &[u8]) {
66        let length = data.len() as _;
67        unsafe {
68            ffi::g_checksum_update(self.to_glib_none_mut().0, data.to_glib_none().0, length);
69        }
70    }
71
72    /// Gets the length in bytes of digests of type @checksum_type
73    /// ## `checksum_type`
74    /// a #GChecksumType
75    ///
76    /// # Returns
77    ///
78    /// the checksum length, or -1 if @checksum_type is
79    /// not supported.
80    #[doc(alias = "g_checksum_type_get_length")]
81    pub fn type_get_length(checksum_type: ChecksumType) -> isize {
82        unsafe { ffi::g_checksum_type_get_length(checksum_type.into_glib()) }
83    }
84}
85
86unsafe impl Send for Checksum {}
87unsafe impl Sync for Checksum {}