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 cache: cargo
rust: rust:
- nightly - nightly
- nightly-2017-01-25 - nightly-2017-06-07
- beta - beta
- stable - stable
- 1.13.0 - 1.18.0
matrix: matrix:
allow_failures: allow_failures:
- rust: nightly - rust: nightly

View file

@ -850,12 +850,11 @@ impl<'a> Help<'a> {
_ => continue, _ => continue,
}; };
debugln!("Help::write_template_help:iter: tag_buf={};", unsafe { debugln!(
String::from_utf8_unchecked(tag_buf.get_ref()[0..tag_length] "Help::write_template_help:iter: tag_buf={};",
.iter() str::from_utf8(&tag_buf.get_ref()[..tag_length])
.map(|&i| i) .unwrap_or_default()
.collect::<Vec<_>>()) );
});
match &tag_buf.get_ref()[0..tag_length] { match &tag_buf.get_ref()[0..tag_length] {
b"?" => { b"?" => {
self.writer.write_all(b"Could not decode tag name")?; 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) { match Help::write_parser_help(&mut buf, self, use_long) {
Err(e) => e, Err(e) => e,
_ => Error { _ => Error {
message: unsafe { String::from_utf8_unchecked(buf) }, message: String::from_utf8(buf).unwrap_or_default(),
kind: ErrorKind::HelpDisplayed, kind: ErrorKind::HelpDisplayed,
info: None, info: None,
} }
@ -1566,7 +1566,7 @@ impl<'a, 'b> Parser<'a, 'b>
if no_val && min_vals_zero && !has_eq && needs_eq { if no_val && min_vals_zero && !has_eq && needs_eq {
debugln!("Parser::parse_opt: More arg vals not required..."); debugln!("Parser::parse_opt: More arg vals not required...");
return Ok(ParseResult::ValuesDone); 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..."); debugln!("Parser::parse_opt: More arg vals required...");
return Ok(ParseResult::Opt(opt.b.name)); return Ok(ParseResult::Opt(opt.b.name));
} }

View file

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