Merge pull request #1058 from H2CO3/master

Reduce amount of unsafe code
This commit is contained in:
Kevin K 2017-10-07 12:59:00 -04:00 committed by GitHub
commit 609a1f27a1
4 changed files with 23 additions and 21 deletions

View file

@ -3,10 +3,10 @@ language: rust
cache: cargo
rust:
- nightly
- nightly-2017-01-25
- nightly-2017-06-07
- beta
- stable
- 1.13.0
- 1.18.0
matrix:
allow_failures:
- rust: nightly

View file

@ -850,12 +850,11 @@ impl<'a> Help<'a> {
_ => continue,
};
debugln!("Help::write_template_help:iter: tag_buf={};", unsafe {
String::from_utf8_unchecked(tag_buf.get_ref()[0..tag_length]
.iter()
.map(|&i| i)
.collect::<Vec<_>>())
});
debugln!(
"Help::write_template_help:iter: tag_buf={};",
str::from_utf8(&tag_buf.get_ref()[..tag_length])
.unwrap_or_default()
);
match &tag_buf.get_ref()[0..tag_length] {
b"?" => {
self.writer.write_all(b"Could not decode tag name")?;

View file

@ -1352,7 +1352,7 @@ impl<'a, 'b> Parser<'a, 'b>
match Help::write_parser_help(&mut buf, self, use_long) {
Err(e) => e,
_ => Error {
message: unsafe { String::from_utf8_unchecked(buf) },
message: String::from_utf8(buf).unwrap_or_default(),
kind: ErrorKind::HelpDisplayed,
info: None,
}
@ -1566,7 +1566,7 @@ impl<'a, 'b> Parser<'a, 'b>
if no_val && min_vals_zero && !has_eq && needs_eq {
debugln!("Parser::parse_opt: More arg vals not required...");
return Ok(ParseResult::ValuesDone);
} else if no_val || (mult && !needs_delim) && !has_eq && matcher.needs_more_vals(opt) {
} else if no_val || (mult && !needs_delim) && !has_eq && matcher.needs_more_vals(opt) {
debugln!("Parser::parse_opt: More arg vals required...");
return Ok(ParseResult::Opt(opt.b.name));
}

View file

@ -422,26 +422,29 @@ macro_rules! crate_authors {
use std::sync::{ONCE_INIT, Once};
#[allow(missing_copy_implementations)]
#[allow(non_camel_case_types)]
#[allow(dead_code)]
struct CARGO_AUTHORS {__private_field: ()}
static CARGO_AUTHORS: CARGO_AUTHORS = CARGO_AUTHORS {__private_field: ()};
struct CargoAuthors { __private_field: () };
impl Deref for CARGO_AUTHORS {
type Target = String;
impl Deref for CargoAuthors {
type Target = str;
#[allow(unsafe_code)]
fn deref<'a>(&'a self) -> &'a String {
unsafe {
static mut LAZY: (*const String, Once) = (0 as *const String, ONCE_INIT);
fn deref(&self) -> &'static str {
static ONCE: Once = ONCE_INIT;
static mut VALUE: *const String = 0 as *const String;
LAZY.1.call_once(|| LAZY.0 = Box::into_raw(Box::new(env!("CARGO_PKG_AUTHORS").replace(':', $sep))));
&*LAZY.0
unsafe {
ONCE.call_once(|| {
let s = env!("CARGO_PKG_AUTHORS").replace(':', $sep);
VALUE = Box::into_raw(Box::new(s));
});
&(*VALUE)[..]
}
}
}
&CARGO_AUTHORS[..]
&*CargoAuthors { __private_field: () }
}};
() => {
env!("CARGO_PKG_AUTHORS")