Skip to content

Commit 6b1bdba

Browse files
authored
Merge pull request #1238 from sdroege/data-input-stream-no-const
gio: Don't wrongly cast DataInputStream byte arrays to a const pointer
2 parents a0d5234 + 755e082 commit 6b1bdba

File tree

4 files changed

+180
-147
lines changed

4 files changed

+180
-147
lines changed

gio/Gir.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -772,6 +772,16 @@ manual_traits = ["FileExtManual"]
772772
name = "new_tmp_dir_async"
773773
manual = true
774774
doc_trait_name = "FileExtManual"
775+
[[object.function]]
776+
name = "load_contents"
777+
# Use `Slice<u8>`
778+
manual = true
779+
doc_trait_name = "FileExtManual"
780+
[[object.function]]
781+
name = "load_contents_async"
782+
# Use `Slice<u8>`
783+
manual = true
784+
doc_trait_name = "FileExtManual"
775785

776786
[[object]]
777787
name = "Gio.FileAttributeInfoList"

gio/src/auto/file.rs

Lines changed: 0 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -903,117 +903,6 @@ pub trait FileExt: IsA<File> + sealed::Sealed + 'static {
903903
))
904904
}
905905

906-
#[doc(alias = "g_file_load_contents")]
907-
fn load_contents(
908-
&self,
909-
cancellable: Option<&impl IsA<Cancellable>>,
910-
) -> Result<(Vec<u8>, Option<glib::GString>), glib::Error> {
911-
unsafe {
912-
let mut contents = std::ptr::null_mut();
913-
let mut length = std::mem::MaybeUninit::uninit();
914-
let mut etag_out = std::ptr::null_mut();
915-
let mut error = std::ptr::null_mut();
916-
let is_ok = ffi::g_file_load_contents(
917-
self.as_ref().to_glib_none().0,
918-
cancellable.map(|p| p.as_ref()).to_glib_none().0,
919-
&mut contents,
920-
length.as_mut_ptr(),
921-
&mut etag_out,
922-
&mut error,
923-
);
924-
debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
925-
if error.is_null() {
926-
Ok((
927-
FromGlibContainer::from_glib_full_num(contents, length.assume_init() as _),
928-
from_glib_full(etag_out),
929-
))
930-
} else {
931-
Err(from_glib_full(error))
932-
}
933-
}
934-
}
935-
936-
#[doc(alias = "g_file_load_contents_async")]
937-
fn load_contents_async<
938-
P: FnOnce(Result<(Vec<u8>, Option<glib::GString>), glib::Error>) + 'static,
939-
>(
940-
&self,
941-
cancellable: Option<&impl IsA<Cancellable>>,
942-
callback: P,
943-
) {
944-
let main_context = glib::MainContext::ref_thread_default();
945-
let is_main_context_owner = main_context.is_owner();
946-
let has_acquired_main_context = (!is_main_context_owner)
947-
.then(|| main_context.acquire().ok())
948-
.flatten();
949-
assert!(
950-
is_main_context_owner || has_acquired_main_context.is_some(),
951-
"Async operations only allowed if the thread is owning the MainContext"
952-
);
953-
954-
let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
955-
Box_::new(glib::thread_guard::ThreadGuard::new(callback));
956-
unsafe extern "C" fn load_contents_async_trampoline<
957-
P: FnOnce(Result<(Vec<u8>, Option<glib::GString>), glib::Error>) + 'static,
958-
>(
959-
_source_object: *mut glib::gobject_ffi::GObject,
960-
res: *mut crate::ffi::GAsyncResult,
961-
user_data: glib::ffi::gpointer,
962-
) {
963-
let mut error = std::ptr::null_mut();
964-
let mut contents = std::ptr::null_mut();
965-
let mut length = std::mem::MaybeUninit::uninit();
966-
let mut etag_out = std::ptr::null_mut();
967-
let _ = ffi::g_file_load_contents_finish(
968-
_source_object as *mut _,
969-
res,
970-
&mut contents,
971-
length.as_mut_ptr(),
972-
&mut etag_out,
973-
&mut error,
974-
);
975-
let result = if error.is_null() {
976-
Ok((
977-
FromGlibContainer::from_glib_full_num(contents, length.assume_init() as _),
978-
from_glib_full(etag_out),
979-
))
980-
} else {
981-
Err(from_glib_full(error))
982-
};
983-
let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
984-
Box_::from_raw(user_data as *mut _);
985-
let callback: P = callback.into_inner();
986-
callback(result);
987-
}
988-
let callback = load_contents_async_trampoline::<P>;
989-
unsafe {
990-
ffi::g_file_load_contents_async(
991-
self.as_ref().to_glib_none().0,
992-
cancellable.map(|p| p.as_ref()).to_glib_none().0,
993-
Some(callback),
994-
Box_::into_raw(user_data) as *mut _,
995-
);
996-
}
997-
}
998-
999-
fn load_contents_future(
1000-
&self,
1001-
) -> Pin<
1002-
Box_<
1003-
dyn std::future::Future<Output = Result<(Vec<u8>, Option<glib::GString>), glib::Error>>
1004-
+ 'static,
1005-
>,
1006-
> {
1007-
Box_::pin(crate::GioFuture::new(
1008-
self,
1009-
move |obj, cancellable, send| {
1010-
obj.load_contents_async(Some(cancellable), move |res| {
1011-
send.resolve(res);
1012-
});
1013-
},
1014-
))
1015-
}
1016-
1017906
#[doc(alias = "g_file_make_directory")]
1018907
fn make_directory(
1019908
&self,

gio/src/data_input_stream.rs

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub trait DataInputStreamExtManual: sealed::Sealed + IsA<DataInputStream> + 'sta
1616
fn read_line<P: IsA<Cancellable>>(
1717
&self,
1818
cancellable: Option<&P>,
19-
) -> Result<Vec<u8>, glib::Error> {
19+
) -> Result<glib::collections::Slice<u8>, glib::Error> {
2020
unsafe {
2121
let mut length = mem::MaybeUninit::uninit();
2222
let mut error = ptr::null_mut();
@@ -36,7 +36,10 @@ pub trait DataInputStreamExtManual: sealed::Sealed + IsA<DataInputStream> + 'sta
3636
}
3737

3838
#[doc(alias = "g_data_input_stream_read_line_async")]
39-
fn read_line_async<P: IsA<Cancellable>, Q: FnOnce(Result<Vec<u8>, glib::Error>) + 'static>(
39+
fn read_line_async<
40+
P: IsA<Cancellable>,
41+
Q: FnOnce(Result<glib::collections::Slice<u8>, glib::Error>) + 'static,
42+
>(
4043
&self,
4144
io_priority: glib::Priority,
4245
cancellable: Option<&P>,
@@ -55,7 +58,7 @@ pub trait DataInputStreamExtManual: sealed::Sealed + IsA<DataInputStream> + 'sta
5558
let user_data: Box_<glib::thread_guard::ThreadGuard<Q>> =
5659
Box_::new(glib::thread_guard::ThreadGuard::new(callback));
5760
unsafe extern "C" fn read_line_async_trampoline<
58-
Q: FnOnce(Result<Vec<u8>, glib::Error>) + 'static,
61+
Q: FnOnce(Result<glib::collections::Slice<u8>, glib::Error>) + 'static,
5962
>(
6063
_source_object: *mut glib::gobject_ffi::GObject,
6164
res: *mut ffi::GAsyncResult,
@@ -95,7 +98,12 @@ pub trait DataInputStreamExtManual: sealed::Sealed + IsA<DataInputStream> + 'sta
9598
fn read_line_future(
9699
&self,
97100
io_priority: glib::Priority,
98-
) -> Pin<Box_<dyn std::future::Future<Output = Result<Vec<u8>, glib::Error>> + 'static>> {
101+
) -> Pin<
102+
Box_<
103+
dyn std::future::Future<Output = Result<glib::collections::Slice<u8>, glib::Error>>
104+
+ 'static,
105+
>,
106+
> {
99107
Box_::pin(crate::GioFuture::new(
100108
self,
101109
move |obj, cancellable, send| {
@@ -204,7 +212,7 @@ pub trait DataInputStreamExtManual: sealed::Sealed + IsA<DataInputStream> + 'sta
204212
&self,
205213
stop_chars: &[u8],
206214
cancellable: Option<&P>,
207-
) -> Result<Vec<u8>, glib::Error> {
215+
) -> Result<glib::collections::Slice<u8>, glib::Error> {
208216
let stop_chars_len = stop_chars.len() as isize;
209217
unsafe {
210218
let mut error = ptr::null_mut();
@@ -220,7 +228,7 @@ pub trait DataInputStreamExtManual: sealed::Sealed + IsA<DataInputStream> + 'sta
220228
if error.is_null() {
221229
let length = length.assume_init();
222230
Ok(FromGlibContainer::from_glib_full_num(
223-
ret as *const _,
231+
ret as *mut u8,
224232
length,
225233
))
226234
} else {
@@ -230,7 +238,10 @@ pub trait DataInputStreamExtManual: sealed::Sealed + IsA<DataInputStream> + 'sta
230238
}
231239

232240
#[doc(alias = "g_data_input_stream_read_upto_async")]
233-
fn read_upto_async<P: IsA<Cancellable>, Q: FnOnce(Result<Vec<u8>, glib::Error>) + 'static>(
241+
fn read_upto_async<
242+
P: IsA<Cancellable>,
243+
Q: FnOnce(Result<glib::collections::Slice<u8>, glib::Error>) + 'static,
244+
>(
234245
&self,
235246
stop_chars: &[u8],
236247
io_priority: glib::Priority,
@@ -251,7 +262,7 @@ pub trait DataInputStreamExtManual: sealed::Sealed + IsA<DataInputStream> + 'sta
251262
let user_data: Box_<glib::thread_guard::ThreadGuard<Q>> =
252263
Box_::new(glib::thread_guard::ThreadGuard::new(callback));
253264
unsafe extern "C" fn read_upto_async_trampoline<
254-
Q: FnOnce(Result<Vec<u8>, glib::Error>) + 'static,
265+
Q: FnOnce(Result<glib::collections::Slice<u8>, glib::Error>) + 'static,
255266
>(
256267
_source_object: *mut glib::gobject_ffi::GObject,
257268
res: *mut ffi::GAsyncResult,
@@ -268,7 +279,7 @@ pub trait DataInputStreamExtManual: sealed::Sealed + IsA<DataInputStream> + 'sta
268279
let result = if error.is_null() {
269280
let length = length.assume_init();
270281
Ok(FromGlibContainer::from_glib_full_num(
271-
ret as *const _,
282+
ret as *mut u8,
272283
length,
273284
))
274285
} else {
@@ -297,7 +308,12 @@ pub trait DataInputStreamExtManual: sealed::Sealed + IsA<DataInputStream> + 'sta
297308
&self,
298309
stop_chars: &[u8],
299310
io_priority: glib::Priority,
300-
) -> Pin<Box_<dyn std::future::Future<Output = Result<Vec<u8>, glib::Error>> + 'static>> {
311+
) -> Pin<
312+
Box_<
313+
dyn std::future::Future<Output = Result<glib::collections::Slice<u8>, glib::Error>>
314+
+ 'static,
315+
>,
316+
> {
301317
let stop_chars = Vec::from(stop_chars);
302318
Box_::pin(crate::GioFuture::new(
303319
self,

0 commit comments

Comments
 (0)