From 7ba1150943b72f1da0cce2aa20fdd7662fa89449 Mon Sep 17 00:00:00 2001 From: Mike Blumenkrantz Date: Thu, 13 Nov 2014 14:38:54 -0500 Subject: [PATCH] embedding: add eutil::slice_to_str() convenience function converts a *u8+length to &str and returns c_int --- ports/cef/eutil.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/ports/cef/eutil.rs b/ports/cef/eutil.rs index b71421f8c40..30718f60e99 100644 --- a/ports/cef/eutil.rs +++ b/ports/cef/eutil.rs @@ -2,6 +2,23 @@ * 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 std::str; + pub fn fptr_is_null(fptr: *const u8) -> bool { fptr.is_null() } + +pub fn slice_to_str(s: *const u8, l: uint, f: |&str| -> c_int) -> c_int { + unsafe { + slice::raw::buf_as_slice(s, l, |result| { + match str::from_utf8(result) { + Some(ruststr) => { + f(ruststr) + }, + None => 0 + } + }) + } +}