gio/auto/
memory_output_stream.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::{ffi, OutputStream, PollableOutputStream, Seekable};
6use glib::{
7    prelude::*,
8    signal::{connect_raw, SignalHandlerId},
9    translate::*,
10};
11use std::boxed::Box as Box_;
12
13glib::wrapper! {
14    /// `GMemoryOutputStream` is a class for using arbitrary
15    /// memory chunks as output for GIO streaming output operations.
16    ///
17    /// As of GLib 2.34, `GMemoryOutputStream` trivially implements
18    /// [`PollableOutputStream`][crate::PollableOutputStream]: it always polls as ready.
19    ///
20    /// ## Properties
21    ///
22    ///
23    /// #### `data`
24    ///  Pointer to buffer where data will be written.
25    ///
26    /// Readable | Writeable | Construct Only
27    ///
28    ///
29    /// #### `data-size`
30    ///  Size of data written to the buffer.
31    ///
32    /// Readable
33    ///
34    ///
35    /// #### `destroy-function`
36    ///  Function called with the buffer as argument when the stream is destroyed.
37    ///
38    /// Readable | Writeable | Construct Only
39    ///
40    ///
41    /// #### `realloc-function`
42    ///  Function with realloc semantics called to enlarge the buffer.
43    ///
44    /// Readable | Writeable | Construct Only
45    ///
46    ///
47    /// #### `size`
48    ///  Current size of the data buffer.
49    ///
50    /// Readable | Writeable | Construct Only
51    ///
52    /// # Implements
53    ///
54    /// [`MemoryOutputStreamExt`][trait@crate::prelude::MemoryOutputStreamExt], [`OutputStreamExt`][trait@crate::prelude::OutputStreamExt], [`trait@glib::ObjectExt`], [`PollableOutputStreamExt`][trait@crate::prelude::PollableOutputStreamExt], [`SeekableExt`][trait@crate::prelude::SeekableExt], [`OutputStreamExtManual`][trait@crate::prelude::OutputStreamExtManual], [`PollableOutputStreamExtManual`][trait@crate::prelude::PollableOutputStreamExtManual]
55    #[doc(alias = "GMemoryOutputStream")]
56    pub struct MemoryOutputStream(Object<ffi::GMemoryOutputStream, ffi::GMemoryOutputStreamClass>) @extends OutputStream, @implements PollableOutputStream, Seekable;
57
58    match fn {
59        type_ => || ffi::g_memory_output_stream_get_type(),
60    }
61}
62
63impl MemoryOutputStream {
64    pub const NONE: Option<&'static MemoryOutputStream> = None;
65
66    /// Creates a new #GMemoryOutputStream, using g_realloc() and g_free()
67    /// for memory allocation.
68    #[doc(alias = "g_memory_output_stream_new_resizable")]
69    pub fn new_resizable() -> MemoryOutputStream {
70        unsafe {
71            OutputStream::from_glib_full(ffi::g_memory_output_stream_new_resizable()).unsafe_cast()
72        }
73    }
74}
75
76mod sealed {
77    pub trait Sealed {}
78    impl<T: super::IsA<super::MemoryOutputStream>> Sealed for T {}
79}
80
81/// Trait containing all [`struct@MemoryOutputStream`] methods.
82///
83/// # Implementors
84///
85/// [`MemoryOutputStream`][struct@crate::MemoryOutputStream]
86pub trait MemoryOutputStreamExt: IsA<MemoryOutputStream> + sealed::Sealed + 'static {
87    /// Returns the number of bytes from the start up to including the last
88    /// byte written in the stream that has not been truncated away.
89    ///
90    /// # Returns
91    ///
92    /// the number of bytes written to the stream
93    #[doc(alias = "g_memory_output_stream_get_data_size")]
94    #[doc(alias = "get_data_size")]
95    #[doc(alias = "data-size")]
96    fn data_size(&self) -> usize {
97        unsafe { ffi::g_memory_output_stream_get_data_size(self.as_ref().to_glib_none().0) }
98    }
99
100    /// Returns data from the @self as a #GBytes. @self must be
101    /// closed before calling this function.
102    ///
103    /// # Returns
104    ///
105    /// the stream's data
106    #[doc(alias = "g_memory_output_stream_steal_as_bytes")]
107    fn steal_as_bytes(&self) -> glib::Bytes {
108        unsafe {
109            from_glib_full(ffi::g_memory_output_stream_steal_as_bytes(
110                self.as_ref().to_glib_none().0,
111            ))
112        }
113    }
114
115    #[doc(alias = "data-size")]
116    fn connect_data_size_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
117        unsafe extern "C" fn notify_data_size_trampoline<
118            P: IsA<MemoryOutputStream>,
119            F: Fn(&P) + 'static,
120        >(
121            this: *mut ffi::GMemoryOutputStream,
122            _param_spec: glib::ffi::gpointer,
123            f: glib::ffi::gpointer,
124        ) {
125            let f: &F = &*(f as *const F);
126            f(MemoryOutputStream::from_glib_borrow(this).unsafe_cast_ref())
127        }
128        unsafe {
129            let f: Box_<F> = Box_::new(f);
130            connect_raw(
131                self.as_ptr() as *mut _,
132                b"notify::data-size\0".as_ptr() as *const _,
133                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
134                    notify_data_size_trampoline::<Self, F> as *const (),
135                )),
136                Box_::into_raw(f),
137            )
138        }
139    }
140}
141
142impl<O: IsA<MemoryOutputStream>> MemoryOutputStreamExt for O {}