gecko-dev/servo/ports/cef/string_list.rs
Simon Sapin 610a77ac31 servo: Merge #18839 - Remove some usage of unstable features (from servo:stabler); r=nox
`Box::new(x)` seems to compile to exactly the same assembly as `box x` in release mode:
https://play.rust-lang.org/?gist=55ec5dd02c22fa799375d6e2e1a6e970&version=nightly

Source-Repo: https://github.com/servo/servo
Source-Revision: fc4de5adf5bff7fabe9f32b960c405f249b5d035

--HG--
extra : subtree_source : https%3A//hg.mozilla.org/projects/converted-servo-linear
extra : subtree_revision : 029931b3a8d9c2e073038623375ebbc38eba348a
2017-10-12 05:18:00 -05:00

69 lines
2.0 KiB
Rust

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use libc::{c_int};
use std::slice;
use string::cef_string_utf16_set;
use types::{cef_string_list_t,cef_string_t};
//cef_string_list
#[no_mangle]
pub extern "C" fn cef_string_list_alloc() -> *mut cef_string_list_t {
Box::into_raw(Box::new(vec!()))
}
#[no_mangle]
pub extern "C" fn cef_string_list_size(lt: *mut cef_string_list_t) -> c_int {
unsafe {
if lt.is_null() { return 0; }
(*lt).len() as c_int
}
}
#[no_mangle]
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; }
(*lt).push(String::from_utf16(slice::from_raw_parts((*value).str, (*value).length as usize)).unwrap());
}
}
#[no_mangle]
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; }
if index as usize > (*lt).len() - 1 { return 0; }
let ref string = (*lt)[index as usize];
let utf16_chars: Vec<u16> = string.encode_utf16().collect();
cef_string_utf16_set(utf16_chars.as_ptr(), utf16_chars.len(), value, 1)
}
}
#[no_mangle]
pub extern "C" fn cef_string_list_clear(lt: *mut cef_string_list_t) {
unsafe {
if lt.is_null() { return; }
(*lt).clear();
}
}
#[no_mangle]
pub extern "C" fn cef_string_list_free(lt: *mut cef_string_list_t) {
unsafe {
if lt.is_null() { return; }
cef_string_list_clear(lt);
drop(Box::from_raw(lt));
}
}
#[no_mangle]
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 copy = (*lt).clone();
Box::into_raw(Box::new(copy))
}
}