Auto merge of #7523 - eefriedman:unnecessary-unsafe, r=SimonSapin

Fix up some unnecessary uses of `unsafe`.



<!-- Reviewable:start -->
[<img src="https://reviewable.io/review_button.png" height=40 alt="Review on Reviewable"/>](https://reviewable.io/reviews/servo/servo/7523)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2015-09-09 00:05:17 -06:00
commit be9a9ffda1
8 changed files with 208 additions and 226 deletions

View file

@ -6,9 +6,8 @@ use euclid::{Point2D, Rect, Size2D};
use smallvec::SmallVec;
use std::borrow::ToOwned;
use std::cell::RefCell;
use std::mem;
use std::rc::Rc;
use std::slice;
use std::str;
use std::sync::Arc;
use style::computed_values::{font_stretch, font_variant, font_weight};
use style::properties::style_structs::Font as FontStyle;
@ -56,12 +55,11 @@ pub trait FontTableTagConversions {
impl FontTableTagConversions for FontTableTag {
fn tag_to_str(&self) -> String {
unsafe {
let pointer = mem::transmute::<&u32, *const u8>(self);
let mut bytes = slice::from_raw_parts(pointer, 4).to_vec();
bytes.reverse();
String::from_utf8_unchecked(bytes)
}
let bytes = [(self >> 24) as u8,
(self >> 16) as u8,
(self >> 8) as u8,
(self >> 0) as u8];
str::from_utf8(&bytes).unwrap().to_owned()
}
}

View file

@ -81,15 +81,11 @@ pub enum Command {
Exit(Sender<()>),
}
unsafe impl Send for Command {}
/// Reply messages sent from the font cache task to the FontContext caller.
pub enum Reply {
GetFontTemplateReply(Option<Arc<FontTemplateData>>),
}
unsafe impl Send for Reply {}
/// The font cache task itself. It maintains a list of reference counted
/// font templates that are currently in use.
struct FontCache {

View file

@ -23,13 +23,12 @@ use selectors::parser::PseudoElement;
use selectors::{Element};
use std::borrow::ToOwned;
use std::hash::{Hash, Hasher};
use std::mem;
use std::slice::Iter;
use std::sync::Arc;
use std::sync::mpsc::Sender;
use string_cache::{Atom, Namespace};
use style::node::TElementAttributes;
use style::properties::{ComputedValues, cascade};
use style::properties::{ComputedValues, cascade, PropertyDeclaration};
use style::selector_matching::{Stylist, DeclarationBlock};
use util::arc_ptr_eq;
use util::cache::{LRUCache, SimpleHashCache};
@ -128,9 +127,9 @@ impl<'a> PartialEq<ApplicableDeclarationsCacheEntry> for ApplicableDeclarationsC
impl<'a> Hash for ApplicableDeclarationsCacheQuery<'a> {
fn hash<H: Hasher>(&self, state: &mut H) {
for declaration in self.declarations {
let ptr: usize = unsafe {
mem::transmute_copy(declaration)
};
// Each declaration contians an Arc, which is a stable
// pointer; we use that for hashing and equality.
let ptr = &*declaration.declarations as *const Vec<PropertyDeclaration>;
ptr.hash(state);
}
}

View file

@ -105,7 +105,7 @@ fn read_block<R: Read>(reader: &mut R) -> Result<ReadResult, ()> {
match reader.read(&mut buf) {
Ok(len) if len > 0 => {
unsafe { buf.set_len(len); }
buf.truncate(len);
Ok(ReadResult::Payload(buf))
}
Ok(_) => Ok(ReadResult::EOF),

View file

@ -2,7 +2,7 @@
* 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/. */
#![allow(unsafe_code, unrooted_must_root)]
#![allow(unrooted_must_root)]
use document_loader::DocumentLoader;
use dom::bindings::codegen::Bindings::DocumentBinding::DocumentMethods;

View file

@ -4,7 +4,6 @@
//! A persistent, thread-safe singly-linked list.
use std::mem;
use std::sync::Arc;
pub struct PersistentList<T> {
@ -81,15 +80,7 @@ impl<'a, T> Iterator for PersistentListIterator<'a, T> where T: Send + Sync + 's
fn next(&mut self) -> Option<&'a T> {
let entry = match self.entry {
None => return None,
Some(entry) => {
// This `transmute` is necessary to ensure that the lifetimes of the next entry and
// this entry match up; the compiler doesn't know this, but we do because of the
// reference counting behavior of `Arc`.
unsafe {
mem::transmute::<&'a PersistentListEntry<T>,
&'static PersistentListEntry<T>>(entry)
}
}
Some(entry) => entry,
};
let value = &entry.value;
self.entry = match entry.next {

View file

@ -6,7 +6,7 @@ use std::cell::RefCell;
use std::rc::Rc;
use std::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT, Ordering};
static mut next_tid: AtomicUsize = ATOMIC_USIZE_INIT;
static NEXT_TID: AtomicUsize = ATOMIC_USIZE_INIT;
thread_local!(static TASK_LOCAL_TID: Rc<RefCell<Option<usize>>> = Rc::new(RefCell::new(None)));
@ -15,7 +15,7 @@ pub fn tid() -> usize {
TASK_LOCAL_TID.with(|ref k| {
let ret =
match *k.borrow() {
None => unsafe { next_tid.fetch_add(1, Ordering::SeqCst) },
None => NEXT_TID.fetch_add(1, Ordering::SeqCst),
Some(x) => x,
};