From d7dc85d80e7ee95c03d6028300887bc2fceffbe6 Mon Sep 17 00:00:00 2001 From: Ms2ger Date: Sat, 20 Jun 2015 13:33:25 +0200 Subject: [PATCH 01/12] Use slice::from_raw_parts to simplify command_line_init. --- ports/cef/command_line.rs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/ports/cef/command_line.rs b/ports/cef/command_line.rs index 9af3709f4cd..7f70c77bafa 100644 --- a/ports/cef/command_line.rs +++ b/ports/cef/command_line.rs @@ -32,12 +32,11 @@ fn command_line_new() -> *mut command_line_t { pub fn command_line_init(argc: c_int, argv: *const *const u8) { unsafe { - let mut a: Vec = vec!(); - for i in 0..(argc as usize) { - let slice = ffi::CStr::from_ptr(*argv.offset(i as isize) as *const c_char); - let s = str::from_utf8(slice.to_bytes()).unwrap(); - a.push(s.to_owned()); - } + let args = slice::from_raw_parts(argv, argc as usize); + let a = args.iter().map(|&arg| { + let slice = ffi::CStr::from_ptr(arg as *const c_char); + str::from_utf8(slice.to_bytes()).unwrap().to_owned() + }).collect(); let cl = command_line_new(); (*cl).argc = argc; (*cl).argv = a; From 611a0cd117bb9743185e8b4458b86d80bdef9bd3 Mon Sep 17 00:00:00 2001 From: Ms2ger Date: Sat, 20 Jun 2015 13:34:41 +0200 Subject: [PATCH 02/12] Assign None into CEF string drop fields rather than transmuting a null pointer. --- ports/cef/string.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/ports/cef/string.rs b/ports/cef/string.rs index 523c4f66d2d..c2084bea08c 100644 --- a/ports/cef/string.rs +++ b/ports/cef/string.rs @@ -71,7 +71,7 @@ pub extern "C" fn cef_string_utf8_clear(cs: *mut cef_string_utf8_t) { (*cs).dtor.map(|dtor| dtor((*cs).str)); (*cs).length = 0; (*cs).str = 0 as *mut u8; - (*cs).dtor = mem::transmute(0 as *const u8); + (*cs).dtor = None; } } @@ -101,7 +101,7 @@ pub extern "C" fn cef_string_utf8_set(src: *const u8, src_len: size_t, output: * } else { (*output).str = mem::transmute(src); (*output).length = src_len; - (*output).dtor = mem::transmute(0 as *const u8); + (*output).dtor = None; } } return 1; @@ -151,7 +151,7 @@ pub extern "C" fn cef_string_utf16_clear(cs: *mut cef_string_utf16_t) { (*cs).dtor.map(|dtor| dtor((*cs).str)); (*cs).length = 0; (*cs).str = 0 as *mut c_ushort; - (*cs).dtor = mem::transmute(0 as *const u8); + (*cs).dtor = None; } } @@ -182,7 +182,7 @@ pub extern "C" fn cef_string_utf16_set(src: *const c_ushort, src_len: size_t, ou } else { (*output).str = mem::transmute(src); (*output).length = src_len; - (*output).dtor = mem::transmute(0 as *const u8); + (*output).dtor = None; } } return 1; @@ -209,7 +209,7 @@ pub extern "C" fn cef_string_wide_clear(cs: *mut cef_string_wide_t) { (*cs).dtor.map(|dtor| dtor((*cs).str)); (*cs).length = 0; (*cs).str = 0 as *mut wchar_t; - (*cs).dtor = mem::transmute(0 as *const u8); + (*cs).dtor = None; } } @@ -240,7 +240,7 @@ pub extern "C" fn cef_string_wide_set(src: *const wchar_t, src_len: size_t, outp } else { (*output).str = mem::transmute(src); (*output).length = src_len; - (*output).dtor = mem::transmute(0 as *const u8); + (*output).dtor = None; } } return 1; From 2248e1710a0a22631bbcbe071fc058a59bcefcb5 Mon Sep 17 00:00:00 2001 From: Ms2ger Date: Sat, 20 Jun 2015 13:38:04 +0200 Subject: [PATCH 03/12] Use Box and boxed functions to manage cef_string_list_t memory. --- ports/cef/string_list.rs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/ports/cef/string_list.rs b/ports/cef/string_list.rs index a9a2dfc5252..2c1d4be17be 100644 --- a/ports/cef/string_list.rs +++ b/ports/cef/string_list.rs @@ -3,6 +3,7 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ use libc::{c_int}; +use std::boxed; use std::mem; use std::slice; use string::cef_string_utf16_set; @@ -18,10 +19,7 @@ fn string_list_to_vec(lt: *mut cef_string_list_t) -> *mut Vec { #[no_mangle] pub extern "C" fn cef_string_list_alloc() -> *mut cef_string_list_t { - unsafe { - let lt: Box> = box vec!(); - mem::transmute(lt) - } + boxed::into_raw(box vec!()) } #[no_mangle] @@ -67,9 +65,8 @@ pub extern "C" fn cef_string_list_clear(lt: *mut cef_string_list_t) { pub extern "C" fn cef_string_list_free(lt: *mut cef_string_list_t) { unsafe { if lt.is_null() { return; } - let v: Box> = mem::transmute(lt); cef_string_list_clear(lt); - drop(v); + drop(Box::from_raw(lt)); } } @@ -79,6 +76,6 @@ pub extern "C" fn cef_string_list_copy(lt: *mut cef_string_list_t) -> *mut cef_s if lt.is_null() { return 0 as *mut cef_string_list_t; } let v = string_list_to_vec(lt); let copy = (*v).clone(); - mem::transmute(box copy) + boxed::into_raw(box copy) } } From 4a7c5c80420ae713177b97e701a246d25645c7ef Mon Sep 17 00:00:00 2001 From: Ms2ger Date: Sat, 20 Jun 2015 13:39:05 +0200 Subject: [PATCH 04/12] Remove no-op string_list_to_vec function. --- ports/cef/string_list.rs | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/ports/cef/string_list.rs b/ports/cef/string_list.rs index 2c1d4be17be..e10df59bc56 100644 --- a/ports/cef/string_list.rs +++ b/ports/cef/string_list.rs @@ -11,10 +11,6 @@ use types::{cef_string_list_t,cef_string_t}; use rustc_unicode::str::Utf16Encoder; -fn string_list_to_vec(lt: *mut cef_string_list_t) -> *mut Vec { - lt as *mut Vec -} - //cef_string_list #[no_mangle] @@ -26,8 +22,7 @@ pub extern "C" fn cef_string_list_alloc() -> *mut cef_string_list_t { pub extern "C" fn cef_string_list_size(lt: *mut cef_string_list_t) -> c_int { unsafe { if lt.is_null() { return 0; } - let v = string_list_to_vec(lt); - (*v).len() as c_int + (*lt).len() as c_int } } @@ -35,8 +30,7 @@ pub extern "C" fn cef_string_list_size(lt: *mut cef_string_list_t) -> c_int { pub extern "C" fn cef_string_list_append(lt: *mut cef_string_list_t, value: *const cef_string_t) { unsafe { if lt.is_null() { return; } - let v = string_list_to_vec(lt); - (*v).push(String::from_utf16(slice::from_raw_parts((*value).str, (*value).length as usize)).unwrap()); + (*lt).push(String::from_utf16(slice::from_raw_parts((*value).str, (*value).length as usize)).unwrap()); } } @@ -44,9 +38,8 @@ pub extern "C" fn cef_string_list_append(lt: *mut cef_string_list_t, value: *con pub extern "C" fn cef_string_list_value(lt: *mut cef_string_list_t, index: c_int, value: *mut cef_string_t) -> c_int { unsafe { if index < 0 || lt.is_null() { return 0; } - let v = string_list_to_vec(lt); - if index as usize > (*v).len() - 1 { return 0; } - let ref string = (*v)[index as usize]; + if index as usize > (*lt).len() - 1 { return 0; } + let ref string = (*lt)[index as usize]; let utf16_chars: Vec = Utf16Encoder::new(string.chars()).collect(); cef_string_utf16_set(mem::transmute(utf16_chars.as_ptr()), utf16_chars.len() as u64, value, 1) } @@ -56,8 +49,7 @@ pub extern "C" fn cef_string_list_value(lt: *mut cef_string_list_t, index: c_int pub extern "C" fn cef_string_list_clear(lt: *mut cef_string_list_t) { unsafe { if lt.is_null() { return; } - let v = string_list_to_vec(lt); - (*v).clear(); + (*lt).clear(); } } @@ -74,8 +66,7 @@ pub extern "C" fn cef_string_list_free(lt: *mut cef_string_list_t) { pub extern "C" fn cef_string_list_copy(lt: *mut cef_string_list_t) -> *mut cef_string_list_t { unsafe { if lt.is_null() { return 0 as *mut cef_string_list_t; } - let v = string_list_to_vec(lt); - let copy = (*v).clone(); + let copy = (*lt).clone(); boxed::into_raw(box copy) } } From 2c032edc16795f613ba3239422838f6cb52ec3d3 Mon Sep 17 00:00:00 2001 From: Ms2ger Date: Sat, 20 Jun 2015 13:40:09 +0200 Subject: [PATCH 05/12] Remove unnecessary transmute in cef_string_list_value. --- ports/cef/string_list.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/ports/cef/string_list.rs b/ports/cef/string_list.rs index e10df59bc56..26897552ea3 100644 --- a/ports/cef/string_list.rs +++ b/ports/cef/string_list.rs @@ -4,7 +4,6 @@ use libc::{c_int}; use std::boxed; -use std::mem; use std::slice; use string::cef_string_utf16_set; use types::{cef_string_list_t,cef_string_t}; @@ -41,7 +40,7 @@ pub extern "C" fn cef_string_list_value(lt: *mut cef_string_list_t, index: c_int if index as usize > (*lt).len() - 1 { return 0; } let ref string = (*lt)[index as usize]; let utf16_chars: Vec = Utf16Encoder::new(string.chars()).collect(); - cef_string_utf16_set(mem::transmute(utf16_chars.as_ptr()), utf16_chars.len() as u64, value, 1) + cef_string_utf16_set(utf16_chars.as_ptr(), utf16_chars.len() as u64, value, 1) } } From 893fc18c71e8c1296ed265c732a176e6a29e969f Mon Sep 17 00:00:00 2001 From: Ms2ger Date: Sat, 20 Jun 2015 13:57:48 +0200 Subject: [PATCH 06/12] Remove unnecessary transmutes from CEF strings. --- ports/cef/string.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/ports/cef/string.rs b/ports/cef/string.rs index c2084bea08c..d337346deef 100644 --- a/ports/cef/string.rs +++ b/ports/cef/string.rs @@ -99,7 +99,7 @@ pub extern "C" fn cef_string_utf8_set(src: *const u8, src_len: size_t, output: * (*output).dtor = Some(string_utf8_dtor as extern "C" fn(*mut u8)); } } else { - (*output).str = mem::transmute(src); + (*output).str = src as *mut _; (*output).length = src_len; (*output).dtor = None; } @@ -180,7 +180,7 @@ pub extern "C" fn cef_string_utf16_set(src: *const c_ushort, src_len: size_t, ou (*output).dtor = Some(string_utf16_dtor as extern "C" fn(*mut c_ushort)); } } else { - (*output).str = mem::transmute(src); + (*output).str = src as *mut _; (*output).length = src_len; (*output).dtor = None; } @@ -238,7 +238,7 @@ pub extern "C" fn cef_string_wide_set(src: *const wchar_t, src_len: size_t, outp (*output).dtor = Some(string_wide_dtor as extern "C" fn(*mut wchar_t)); } } else { - (*output).str = mem::transmute(src); + (*output).str = src as *mut _; (*output).length = src_len; (*output).dtor = None; } @@ -311,8 +311,8 @@ pub fn empty_utf16_string() -> cef_string_utf16_t { pub fn string_to_userfree_string(string: cef_string_utf16_t) -> cef_string_userfree_utf16_t { unsafe { - let allocation: cef_string_userfree_utf16_t = - mem::transmute(libc::malloc(mem::size_of::() as size_t)); + let allocation = libc::malloc(mem::size_of::() as size_t) + as cef_string_userfree_utf16_t; ptr::write(allocation, string); allocation } From 577407fe6874a953b61e1ad39614da65b923ad83 Mon Sep 17 00:00:00 2001 From: Ms2ger Date: Sat, 20 Jun 2015 13:58:17 +0200 Subject: [PATCH 07/12] Improve indentation in CEF strings. --- ports/cef/string.rs | 64 ++++++++++++++++++++++----------------------- 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/ports/cef/string.rs b/ports/cef/string.rs index d337346deef..6e5324d3f7c 100644 --- a/ports/cef/string.rs +++ b/ports/cef/string.rs @@ -89,19 +89,19 @@ pub extern "C" fn cef_string_utf8_set(src: *const u8, src_len: size_t, output: * unsafe { if copy != 0 { if !src.is_null() && src_len > 0 { - (*output).str = libc::calloc(1, src_len + 1) as *mut u8; - if (*output).str.is_null() { - return 0; - } + (*output).str = libc::calloc(1, src_len + 1) as *mut u8; + if (*output).str.is_null() { + return 0; + } - ptr::copy(src, (*output).str, src_len as usize); - (*output).length = src_len; - (*output).dtor = Some(string_utf8_dtor as extern "C" fn(*mut u8)); + ptr::copy(src, (*output).str, src_len as usize); + (*output).length = src_len; + (*output).dtor = Some(string_utf8_dtor as extern "C" fn(*mut u8)); } } else { - (*output).str = src as *mut _; - (*output).length = src_len; - (*output).dtor = None; + (*output).str = src as *mut _; + (*output).length = src_len; + (*output).dtor = None; } } return 1; @@ -169,20 +169,20 @@ pub extern "C" fn cef_string_utf16_set(src: *const c_ushort, src_len: size_t, ou unsafe { if copy != 0 { if !src.is_null() && src_len > 0 { - (*output).str = libc::calloc(1, (src_len + 1) * mem::size_of::() as u64) as - *mut u16; - if (*output).str.is_null() { - return 0; - } + (*output).str = libc::calloc(1, (src_len + 1) * mem::size_of::() as u64) as + *mut u16; + if (*output).str.is_null() { + return 0; + } - ptr::copy(src, (*output).str, src_len as usize); - (*output).length = src_len; - (*output).dtor = Some(string_utf16_dtor as extern "C" fn(*mut c_ushort)); + ptr::copy(src, (*output).str, src_len as usize); + (*output).length = src_len; + (*output).dtor = Some(string_utf16_dtor as extern "C" fn(*mut c_ushort)); } } else { - (*output).str = src as *mut _; - (*output).length = src_len; - (*output).dtor = None; + (*output).str = src as *mut _; + (*output).length = src_len; + (*output).dtor = None; } } return 1; @@ -227,20 +227,20 @@ pub extern "C" fn cef_string_wide_set(src: *const wchar_t, src_len: size_t, outp unsafe { if copy != 0 { if !src.is_null() && src_len > 0 { - (*output).str = libc::calloc(1, (src_len + 1) * mem::size_of::() as u64) as - *mut wchar_t; - if (*output).str.is_null() { - return 0; - } + (*output).str = libc::calloc(1, (src_len + 1) * mem::size_of::() as u64) as + *mut wchar_t; + if (*output).str.is_null() { + return 0; + } - ptr::copy(src, (*output).str, src_len as usize); - (*output).length = src_len; - (*output).dtor = Some(string_wide_dtor as extern "C" fn(*mut wchar_t)); + ptr::copy(src, (*output).str, src_len as usize); + (*output).length = src_len; + (*output).dtor = Some(string_wide_dtor as extern "C" fn(*mut wchar_t)); } } else { - (*output).str = src as *mut _; - (*output).length = src_len; - (*output).dtor = None; + (*output).str = src as *mut _; + (*output).length = src_len; + (*output).dtor = None; } } return 1; From 0f65c04fe92192fa257f413e8a2d10dba1b4aa8e Mon Sep 17 00:00:00 2001 From: Ms2ger Date: Sat, 20 Jun 2015 14:00:31 +0200 Subject: [PATCH 08/12] Use Box and boxed functions to manage cef_string_map_t memory. --- ports/cef/string_map.rs | 9 +++------ ports/cef/types.rs | 2 +- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/ports/cef/string_map.rs b/ports/cef/string_map.rs index b7fd8dffcc3..7e34ca23b1e 100644 --- a/ports/cef/string_map.rs +++ b/ports/cef/string_map.rs @@ -4,8 +4,8 @@ use eutil::slice_to_str; use libc::{c_int}; +use std::boxed; use std::collections::BTreeMap; -use std::mem; use std::string::String; use string::{cef_string_userfree_utf16_alloc, cef_string_userfree_utf16_free}; use string::{cef_string_utf16_set}; @@ -19,10 +19,7 @@ fn string_map_to_treemap(sm: *mut cef_string_map_t) -> *mut BTreeMap *mut cef_string_map_t { - unsafe { - let sm: Box> = box BTreeMap::new(); - mem::transmute(sm) - } + boxed::into_raw(box BTreeMap::new()) } #[no_mangle] @@ -118,7 +115,7 @@ pub extern "C" fn cef_string_map_clear(sm: *mut cef_string_map_t) { pub extern "C" fn cef_string_map_free(sm: *mut cef_string_map_t) { unsafe { if sm.is_null() { return; } - let _v: Box> = mem::transmute(sm); cef_string_map_clear(sm); + drop(Box::from_raw(sm)); } } diff --git a/ports/cef/types.rs b/ports/cef/types.rs index 5378d67b405..be71083f1ed 100644 --- a/ports/cef/types.rs +++ b/ports/cef/types.rs @@ -15,7 +15,7 @@ pub use self::cef_rect as cef_rect_t; use std::collections::BTreeMap; -pub enum cef_string_map_t {} +pub type cef_string_map_t = BTreeMap; pub type cef_string_multimap_t = BTreeMap>; pub type cef_string_list_t = Vec; pub enum cef_text_input_context_t {} From 01df802fb2a5b8649cb7a682337345462c8b8bf5 Mon Sep 17 00:00:00 2001 From: Ms2ger Date: Sat, 20 Jun 2015 14:02:46 +0200 Subject: [PATCH 09/12] Remove the no-op string_map_to_treemap function. --- ports/cef/string_map.rs | 29 +++++++++-------------------- 1 file changed, 9 insertions(+), 20 deletions(-) diff --git a/ports/cef/string_map.rs b/ports/cef/string_map.rs index 7e34ca23b1e..0d01888601e 100644 --- a/ports/cef/string_map.rs +++ b/ports/cef/string_map.rs @@ -6,15 +6,10 @@ use eutil::slice_to_str; use libc::{c_int}; use std::boxed; use std::collections::BTreeMap; -use std::string::String; use string::{cef_string_userfree_utf16_alloc, cef_string_userfree_utf16_free}; use string::{cef_string_utf16_set}; use types::{cef_string_map_t, cef_string_t}; -fn string_map_to_treemap(sm: *mut cef_string_map_t) -> *mut BTreeMap { - sm as *mut BTreeMap -} - //cef_string_map #[no_mangle] @@ -26,8 +21,7 @@ pub extern "C" fn cef_string_map_alloc() -> *mut cef_string_map_t { pub extern "C" fn cef_string_map_size(sm: *mut cef_string_map_t) -> c_int { unsafe { if sm.is_null() { return 0; } - let v = string_map_to_treemap(sm); - (*v).len() as c_int + (*sm).len() as c_int } } @@ -35,11 +29,10 @@ pub extern "C" fn cef_string_map_size(sm: *mut cef_string_map_t) -> c_int { pub extern "C" fn cef_string_map_append(sm: *mut cef_string_map_t, key: *const cef_string_t, value: *const cef_string_t) -> c_int { unsafe { if sm.is_null() { return 0; } - let v = string_map_to_treemap(sm); slice_to_str((*key).str as *const u8, (*key).length as usize, |result| { let csv = cef_string_userfree_utf16_alloc(); cef_string_utf16_set((*value).str as *const u16, (*value).length, csv, 1); - (*v).insert(result.to_owned(), csv); + (*sm).insert(result.to_owned(), csv); 1 }) } @@ -49,9 +42,8 @@ pub extern "C" fn cef_string_map_append(sm: *mut cef_string_map_t, key: *const c pub extern "C" fn cef_string_map_find(sm: *mut cef_string_map_t, key: *const cef_string_t, value: *mut cef_string_t) -> c_int { unsafe { if sm.is_null() { return 0; } - let v = string_map_to_treemap(sm); slice_to_str((*key).str as *const u8, (*key).length as usize, |result| { - match (*v).get(result) { + match (*sm).get(result) { Some(s) => { cef_string_utf16_set((**s).str as *const u16, (**s).length, value, 1); 1 @@ -66,10 +58,9 @@ pub extern "C" fn cef_string_map_find(sm: *mut cef_string_map_t, key: *const cef pub extern "C" fn cef_string_map_key(sm: *mut cef_string_map_t, index: c_int, value: *mut cef_string_t) -> c_int { unsafe { if index < 0 || sm.is_null() { return 0; } - let v = string_map_to_treemap(sm); - if index as usize > (*v).len() - 1 { return 0; } + if index as usize > (*sm).len() - 1 { return 0; } - for (i, k) in (*v).keys().enumerate() { + for (i, k) in (*sm).keys().enumerate() { if i == index as usize { cef_string_utf16_set(k.as_bytes().as_ptr() as *const u16, k.len() as u64, @@ -86,10 +77,9 @@ pub extern "C" fn cef_string_map_key(sm: *mut cef_string_map_t, index: c_int, va pub extern "C" fn cef_string_map_value(sm: *mut cef_string_map_t, index: c_int, value: *mut cef_string_t) -> c_int { unsafe { if index < 0 || sm.is_null() { return 0; } - let v = string_map_to_treemap(sm); - if index as usize > (*v).len() - 1 { return 0; } + if index as usize > (*sm).len() - 1 { return 0; } - for (i, val) in (*v).values().enumerate() { + for (i, val) in (*sm).values().enumerate() { if i == index as usize { cef_string_utf16_set((**val).str as *const u16, (**val).length, value, 1); return 1; @@ -103,11 +93,10 @@ pub extern "C" fn cef_string_map_value(sm: *mut cef_string_map_t, index: c_int, pub extern "C" fn cef_string_map_clear(sm: *mut cef_string_map_t) { unsafe { if sm.is_null() { return; } - let v = string_map_to_treemap(sm); - for val in (*v).values() { + for val in (*sm).values() { cef_string_userfree_utf16_free(*val); } - (*v).clear(); + (*sm).clear(); } } From 26964ff087873bc8567d9beabf48562d10ae2060 Mon Sep 17 00:00:00 2001 From: Ms2ger Date: Sat, 20 Jun 2015 14:03:17 +0200 Subject: [PATCH 10/12] Use nth rather than enumerating in cef_string_map_{key,value}. --- ports/cef/string_map.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/ports/cef/string_map.rs b/ports/cef/string_map.rs index 0d01888601e..2c50d91eb34 100644 --- a/ports/cef/string_map.rs +++ b/ports/cef/string_map.rs @@ -60,17 +60,17 @@ pub extern "C" fn cef_string_map_key(sm: *mut cef_string_map_t, index: c_int, va if index < 0 || sm.is_null() { return 0; } if index as usize > (*sm).len() - 1 { return 0; } - for (i, k) in (*sm).keys().enumerate() { - if i == index as usize { + match (*sm).keys().nth(index as usize) { + Some(k) => { cef_string_utf16_set(k.as_bytes().as_ptr() as *const u16, k.len() as u64, value, 1); - return 1; - } + 1 + }, + None => 0, } } - 0 } #[no_mangle] @@ -79,14 +79,14 @@ pub extern "C" fn cef_string_map_value(sm: *mut cef_string_map_t, index: c_int, if index < 0 || sm.is_null() { return 0; } if index as usize > (*sm).len() - 1 { return 0; } - for (i, val) in (*sm).values().enumerate() { - if i == index as usize { + match (*sm).values().nth(index as usize) { + Some(val) => { cef_string_utf16_set((**val).str as *const u16, (**val).length, value, 1); - return 1; - } + 1 + }, + None => 0, } } - 0 } #[no_mangle] From 49e0442459fe99d034649400b2a1d0bacded6c47 Mon Sep 17 00:00:00 2001 From: Ms2ger Date: Sat, 20 Jun 2015 14:03:39 +0200 Subject: [PATCH 11/12] Use slice::from_raw_parts to convert cef_string_t to &[u16]. --- ports/cef/wrappers.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/ports/cef/wrappers.rs b/ports/cef/wrappers.rs index dcdc686d9e5..ce6873521aa 100644 --- a/ports/cef/wrappers.rs +++ b/ports/cef/wrappers.rs @@ -38,7 +38,7 @@ use std::boxed; use std::collections::HashMap; use std::mem; use std::ptr; -use std::raw; +use std::slice; pub trait CefWrap { fn to_c(rust_object: Self) -> CObject; @@ -209,10 +209,7 @@ impl<'a> CefWrap<*const cef_string_t> for &'a [u16] { } } unsafe fn to_rust(cef_string: *const cef_string_t) -> &'a [u16] { - mem::transmute(raw::Slice { - data: (*cef_string).str, - len: (*cef_string).length as usize, - }) + slice::from_raw_parts((*cef_string).str, (*cef_string).length as usize) } } From da53a9c3015ad0b2a8096c889df16a449d80d9b3 Mon Sep 17 00:00:00 2001 From: Ms2ger Date: Sat, 20 Jun 2015 19:13:42 +0200 Subject: [PATCH 12/12] Return the result of cef_string_utf16_set in string_map. --- ports/cef/string_map.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/ports/cef/string_map.rs b/ports/cef/string_map.rs index 2c50d91eb34..3275fa910c0 100644 --- a/ports/cef/string_map.rs +++ b/ports/cef/string_map.rs @@ -45,8 +45,7 @@ pub extern "C" fn cef_string_map_find(sm: *mut cef_string_map_t, key: *const cef slice_to_str((*key).str as *const u8, (*key).length as usize, |result| { match (*sm).get(result) { Some(s) => { - cef_string_utf16_set((**s).str as *const u16, (**s).length, value, 1); - 1 + cef_string_utf16_set((**s).str as *const u16, (**s).length, value, 1) } None => 0 } @@ -65,8 +64,7 @@ pub extern "C" fn cef_string_map_key(sm: *mut cef_string_map_t, index: c_int, va cef_string_utf16_set(k.as_bytes().as_ptr() as *const u16, k.len() as u64, value, - 1); - 1 + 1) }, None => 0, }