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
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// SPDX-FileCopyrightText: 2022 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
// SPDX-FileContributor: Andrew Hayzen <andrew.hayzen@kdab.com>
//
// SPDX-License-Identifier: MIT OR Apache-2.0

// We are only using references to QUrl so it is actually ffi safe as far as we are concerned
#![allow(improper_ctypes)]

use cxx::{memory::UniquePtrTarget, type_id, ExternType};
use std::{
    ffi::c_void,
    marker::{PhantomData, PhantomPinned},
    mem::MaybeUninit,
};

extern "C" {
    #[link_name = "cxxqt1$qurl$init$from$qurl"]
    fn qurl_init_from_qurl(this: &mut MaybeUninit<cxx::UniquePtr<QUrl>>, qurl: &QUrl);
    #[link_name = "cxxqt1$qurl$init$from$string"]
    fn qurl_init_from_string(
        this: &mut MaybeUninit<cxx::UniquePtr<QUrl>>,
        ptr: *const u8,
        len: usize,
    );
    #[link_name = "cxxqt1$qurl$to$rust$string"]
    fn qurl_to_rust_string(qt: &QUrl, rust: &mut String);
}

/// Binding to Qt `QUrl`.
///
/// # Invariants
///
/// As an invariant of this API and the static analysis of the cxx::bridge
/// macro, in Rust code we can never obtain a `QUrl` by value. Qt's QUrl
/// requires a move constructor and may hold internal pointers, which is not
/// compatible with Rust's move behavior. Instead in Rust code we will only ever
/// look at a QUrl through a reference or smart pointer, as in `&QUrl`
/// or `UniquePtr<QUrl>`.
#[repr(C)]
pub struct QUrl {
    _pinned: PhantomData<PhantomPinned>,
}

impl QUrl {
    /// Create a new Rust Url from this QUrl.
    /// This is a copy operation so any changes will not propagate to the original QUrl.
    pub fn to_rust(&self) -> Url {
        Url::from_qurl(self)
    }
}

// Safety:
//
// The code in this file ensures that QUrl can only ever be allocated
// on the stack in pinned form which avoids the pitfalls of trying to
// move this type that has a non-trivial move constructor.
unsafe impl ExternType for QUrl {
    type Id = type_id!("QUrl");
    type Kind = cxx::kind::Opaque;
}

extern "C" {
    #[link_name = "cxxqt1$unique_ptr$qurl$null"]
    fn unique_ptr_qurl_null(this: *mut MaybeUninit<*mut c_void>);
    #[link_name = "cxxqt1$unique_ptr$qurl$raw"]
    fn unique_ptr_qurl_raw(this: *mut MaybeUninit<*mut c_void>, raw: *mut QUrl);
    #[link_name = "cxxqt1$unique_ptr$qurl$get"]
    fn unique_ptr_qurl_get(this: *const MaybeUninit<*mut c_void>) -> *const QUrl;
    #[link_name = "cxxqt1$unique_ptr$qurl$release"]
    fn unique_ptr_qurl_release(this: *mut MaybeUninit<*mut c_void>) -> *mut QUrl;
    #[link_name = "cxxqt1$unique_ptr$qurl$drop"]
    fn unique_ptr_qurl_drop(this: *mut MaybeUninit<*mut c_void>);
}

// Safety: TODO
unsafe impl UniquePtrTarget for QUrl {
    #[doc(hidden)]
    fn __typename(f: &mut std::fmt::Formatter) -> std::fmt::Result {
        f.write_str("QUrl")
    }

    #[doc(hidden)]
    fn __null() -> MaybeUninit<*mut c_void> {
        let mut repr = MaybeUninit::uninit();
        unsafe {
            unique_ptr_qurl_null(&mut repr);
        }
        repr
    }

    #[doc(hidden)]
    unsafe fn __raw(raw: *mut Self) -> MaybeUninit<*mut c_void> {
        let mut repr = MaybeUninit::uninit();
        unique_ptr_qurl_raw(&mut repr, raw);
        repr
    }

    #[doc(hidden)]
    unsafe fn __get(repr: MaybeUninit<*mut c_void>) -> *const Self {
        unique_ptr_qurl_get(&repr)
    }

    #[doc(hidden)]
    unsafe fn __release(mut repr: MaybeUninit<*mut c_void>) -> *mut Self {
        unique_ptr_qurl_release(&mut repr)
    }

    #[doc(hidden)]
    unsafe fn __drop(mut repr: MaybeUninit<*mut c_void>) {
        unique_ptr_qurl_drop(&mut repr)
    }
}

pub struct Url {
    // Note that once map_qt_value is removed later, this can become private again
    #[doc(hidden)]
    pub(crate) inner: cxx::UniquePtr<QUrl>,
}

impl Url {
    /// Construct a Rust Url from an existing UniquePtr<QUrl> this is a move operation
    ///
    /// This is used in QVariant::value so that we don't need to perform another copy
    pub(crate) fn from_unique_ptr(ptr: cxx::UniquePtr<QUrl>) -> Self {
        Self { inner: ptr }
    }

    pub fn from_qurl(qurl: &QUrl) -> Self {
        Self {
            // Safety: TODO
            inner: unsafe {
                let mut ptr = MaybeUninit::<cxx::UniquePtr<QUrl>>::zeroed();
                qurl_init_from_qurl(&mut ptr, qurl);
                ptr.assume_init()
            },
        }
    }

    // TODO: other QUrl methods
    //
    // fragment: Option<String>,
    // host: Option<String>,
    // password: Option<String>,
    // path: Option<String>,
    // port: Option<u16>,
    // query: Option<String>,
    // scheme: Option<String>,
    // userName: Option<String>,

    pub fn string(&self) -> String {
        let mut s = String::new();
        unsafe { qurl_to_rust_string(&self.inner, &mut s) };
        s
    }
}

impl std::str::FromStr for Url {
    type Err = std::convert::Infallible;

    fn from_str(s: &str) -> Result<Self, std::convert::Infallible> {
        Ok(Self {
            // Safety: TODO
            inner: unsafe {
                let mut ptr = MaybeUninit::<cxx::UniquePtr<QUrl>>::zeroed();
                qurl_init_from_string(&mut ptr, s.as_ptr(), s.len());
                ptr.assume_init()
            },
        })
    }
}

impl crate::ToUniquePtr for Url {
    type CppType = QUrl;

    /// Retrieve the UniquePtr to the Qt QUrl of this Rust Url
    /// so that this object can be passed back to C++.
    fn to_unique_ptr(self) -> cxx::UniquePtr<QUrl> {
        self.inner
    }
}

impl From<&QUrl> for Url {
    fn from(qurl: &QUrl) -> Self {
        qurl.to_rust()
    }
}