diff options
author | Matt A. Tobin <mattatobin@localhost.localdomain> | 2018-02-02 04:16:08 -0500 |
---|---|---|
committer | Matt A. Tobin <mattatobin@localhost.localdomain> | 2018-02-02 04:16:08 -0500 |
commit | 5f8de423f190bbb79a62f804151bc24824fa32d8 (patch) | |
tree | 10027f336435511475e392454359edea8e25895d /netwerk/base/rust-url-capi/src/string_utils.rs | |
parent | 49ee0794b5d912db1f95dce6eb52d781dc210db5 (diff) | |
download | UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.gz UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.lz UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.xz UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.zip |
Add m-esr52 at 52.6.0
Diffstat (limited to 'netwerk/base/rust-url-capi/src/string_utils.rs')
-rw-r--r-- | netwerk/base/rust-url-capi/src/string_utils.rs | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/netwerk/base/rust-url-capi/src/string_utils.rs b/netwerk/base/rust-url-capi/src/string_utils.rs new file mode 100644 index 000000000..ae68a60dc --- /dev/null +++ b/netwerk/base/rust-url-capi/src/string_utils.rs @@ -0,0 +1,57 @@ +extern crate libc; +use libc::size_t; + +extern crate std; +use std::ptr; + +use error_mapping::*; + +extern "C" { + fn c_fn_set_size(user: *mut libc::c_void, size: size_t) -> i32; + fn c_fn_get_buffer(user: *mut libc::c_void) -> *mut libc::c_char; +} + +pub trait StringContainer { + fn set_size(&self, size_t) -> i32; + fn get_buffer(&self) -> *mut libc::c_char; + fn assign(&self, content: &str) -> i32; +} + +impl StringContainer for *mut libc::c_void { + fn set_size(&self, size: size_t) -> i32 { + if (*self).is_null() { + return NSError::InvalidArg.error_code(); + } + unsafe { + c_fn_set_size(*self, size); + } + + return NSError::OK.error_code(); + } + fn get_buffer(&self) -> *mut libc::c_char { + if (*self).is_null() { + return 0 as *mut libc::c_char; + } + unsafe { + c_fn_get_buffer(*self) + } + } + fn assign(&self, content: &str) -> i32 { + if (*self).is_null() { + return NSError::InvalidArg.error_code(); + } + + unsafe { + let slice = content.as_bytes(); + c_fn_set_size(*self, slice.len()); + let buf = c_fn_get_buffer(*self); + if buf.is_null() { + return NSError::Failure.error_code(); + } + + ptr::copy(slice.as_ptr(), buf as *mut u8, slice.len()); + } + + NSError::OK.error_code() + } +} |