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
// Take a look at the license at the top of the repository in the LICENSE file.

use crate::Converter;
use crate::ConverterFlags;
use crate::ConverterResult;
use glib::object::IsA;
use glib::translate::*;
use std::mem;
use std::ptr;

pub trait ConverterExtManual {
    #[doc(alias = "g_converter_convert")]
    fn convert<IN: AsRef<[u8]>, OUT: AsMut<[u8]>>(
        &self,
        inbuf: IN,
        outbuf: OUT,
        flags: ConverterFlags,
    ) -> Result<(ConverterResult, usize, usize), glib::Error>;
}

impl<O: IsA<Converter>> ConverterExtManual for O {
    #[doc(alias = "g_converter_convert")]
    fn convert<IN: AsRef<[u8]>, OUT: AsMut<[u8]>>(
        &self,
        inbuf: IN,
        outbuf: OUT,
        flags: ConverterFlags,
    ) -> Result<(ConverterResult, usize, usize), glib::Error> {
        let inbuf: Box<IN> = Box::new(inbuf);
        let (inbuf_size, inbuf) = {
            let slice = (*inbuf).as_ref();
            (slice.len(), slice.as_ptr())
        };
        let mut outbuf: Box<OUT> = Box::new(outbuf);
        let (outbuf_size, outbuf) = {
            let slice = (*outbuf).as_mut();
            (slice.len(), slice.as_mut_ptr())
        };
        unsafe {
            let mut bytes_read = mem::MaybeUninit::uninit();
            let mut bytes_written = mem::MaybeUninit::uninit();
            let mut error = ptr::null_mut();
            let ret = ffi::g_converter_convert(
                self.as_ref().to_glib_none().0,
                mut_override(inbuf),
                inbuf_size,
                outbuf,
                outbuf_size,
                flags.into_glib(),
                bytes_read.as_mut_ptr(),
                bytes_written.as_mut_ptr(),
                &mut error,
            );
            if error.is_null() {
                Ok((
                    from_glib(ret),
                    bytes_read.assume_init(),
                    bytes_written.assume_init(),
                ))
            } else {
                Err(from_glib_full(error))
            }
        }
    }
}