Skip to content
This repository has been archived by the owner on Jun 8, 2021. It is now read-only.

Create a copy of the Bytes passed to Resource::new_from_data() if it'… #60

Merged
merged 1 commit into from Nov 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion Gir.toml
Expand Up @@ -26,7 +26,6 @@ generate = [
"Gio.MenuModel",
"Gio.NotificationPriority",
"Gio.Permission",
"Gio.Resource",
"Gio.ResourceError",
"Gio.ResourceLookupFlags",
"Gio.SettingsBindFlags",
Expand Down Expand Up @@ -117,3 +116,12 @@ status = "generate"
name = "state"
#value glib::VariantTy
ignore = true

[[object]]
name = "Gio.Resource"
status = "generate"
[[object.function]]
name = "new_from_data"
# Requires special alignment, see
# https://bugzilla.gnome.org/show_bug.cgi?id=790030
ignore = true
8 changes: 0 additions & 8 deletions src/auto/resource.rs
Expand Up @@ -23,14 +23,6 @@ glib_wrapper! {
}

impl Resource {
pub fn new_from_data(data: &glib::Bytes) -> Result<Resource, Error> {
unsafe {
let mut error = ptr::null_mut();
let ret = ffi::g_resource_new_from_data(data.to_glib_none().0, &mut error);
if error.is_null() { Ok(from_glib_full(ret)) } else { Err(from_glib_full(error)) }
}
}

pub fn enumerate_children(&self, path: &str, lookup_flags: ResourceLookupFlags) -> Result<Vec<String>, Error> {
unsafe {
let mut error = ptr::null_mut();
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Expand Up @@ -19,6 +19,7 @@ macro_rules! callback_guard {
}

mod application;
mod resource;

pub use glib::{
Error,
Expand Down
34 changes: 34 additions & 0 deletions src/resource.rs
@@ -0,0 +1,34 @@
// Copyright 2017, The Gtk-rs Project Developers.
// See the COPYRIGHT file at the top-level directory of this distribution.
// Licensed under the MIT license, see the LICENSE file or <http://opensource.org/licenses/MIT>

use Resource;
use ffi;
use glib_ffi;
use glib;
use glib::translate::*;
use std::ptr;
use std::mem;

impl Resource {
pub fn new_from_data(data: &glib::Bytes) -> Result<Resource, glib::Error> {
unsafe {
let mut error = ptr::null_mut();

// Create a copy of data if it is not pointer-aligned
// https://bugzilla.gnome.org/show_bug.cgi?id=790030
let mut data = data.clone();
let data_ptr = glib_ffi::g_bytes_get_data(data.to_glib_none().0, ptr::null_mut());
if data_ptr as usize % mem::align_of::<*const u8>() != 0 {
data = glib::Bytes::from(&*data);
}

let ret = ffi::g_resource_new_from_data(data.to_glib_none().0, &mut error);
if error.is_null() {
Ok(from_glib_full(ret))
} else {
Err(from_glib_full(error))
}
}
}
}