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
76/// Trait containing all [`struct@MemoryOutputStream`] methods.
77///
78/// # Implementors
79///
80/// [`MemoryOutputStream`][struct@crate::MemoryOutputStream]
81pub trait MemoryOutputStreamExt: IsA<MemoryOutputStream> + 'static {
82    /// Returns the number of bytes from the start up to including the last
83    /// byte written in the stream that has not been truncated away.
84    ///
85    /// # Returns
86    ///
87    /// the number of bytes written to the stream
88    #[doc(alias = "g_memory_output_stream_get_data_size")]
89    #[doc(alias = "get_data_size")]
90    #[doc(alias = "data-size")]
91    fn data_size(&self) -> usize {
92        unsafe { ffi::g_memory_output_stream_get_data_size(self.as_ref().to_glib_none().0) }
93    }
94
95    /// Returns data from the @self as a #GBytes. @self must be
96    /// closed before calling this function.
97    ///
98    /// # Returns
99    ///
100    /// the stream's data
101    #[doc(alias = "g_memory_output_stream_steal_as_bytes")]
102    fn steal_as_bytes(&self) -> glib::Bytes {
103        unsafe {
104            from_glib_full(ffi::g_memory_output_stream_steal_as_bytes(
105                self.as_ref().to_glib_none().0,
106            ))
107        }
108    }
109
110    #[doc(alias = "data-size")]
111    fn connect_data_size_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
112        unsafe extern "C" fn notify_data_size_trampoline<
113            P: IsA<MemoryOutputStream>,
114            F: Fn(&P) + 'static,
115        >(
116            this: *mut ffi::GMemoryOutputStream,
117            _param_spec: glib::ffi::gpointer,
118            f: glib::ffi::gpointer,
119        ) {
120            let f: &F = &*(f as *const F);
121            f(MemoryOutputStream::from_glib_borrow(this).unsafe_cast_ref())
122        }
123        unsafe {
124            let f: Box_<F> = Box_::new(f);
125            connect_raw(
126                self.as_ptr() as *mut _,
127                c"notify::data-size".as_ptr() as *const _,
128                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
129                    notify_data_size_trampoline::<Self, F> as *const (),
130                )),
131                Box_::into_raw(f),
132            )
133        }
134    }
135}
136
137impl<O: IsA<MemoryOutputStream>> MemoryOutputStreamExt for O {}