Skip to content

Commit

Permalink
Some TLC for the MoveMap trait
Browse files Browse the repository at this point in the history
  • Loading branch information
Kimundi committed Nov 26, 2015
1 parent 0608b1d commit c56b47a
Show file tree
Hide file tree
Showing 11 changed files with 131 additions and 98 deletions.
44 changes: 7 additions & 37 deletions src/librustc_front/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,7 @@ use syntax::codemap::{respan, Span, Spanned};
use syntax::owned_slice::OwnedSlice;
use syntax::ptr::P;
use syntax::parse::token;
use std::ptr;

// This could have a better place to live.
pub trait MoveMap<T> {
fn move_map<F>(self, f: F) -> Self where F: FnMut(T) -> T;
}

impl<T> MoveMap<T> for Vec<T> {
fn move_map<F>(mut self, mut f: F) -> Vec<T>
where F: FnMut(T) -> T
{
for p in &mut self {
unsafe {
// FIXME(#5016) this shouldn't need to zero to be safe.
ptr::write(p, f(ptr::read_and_drop(p)));
}
}
self
}
}

impl<T> MoveMap<T> for OwnedSlice<T> {
fn move_map<F>(self, f: F) -> OwnedSlice<T>
where F: FnMut(T) -> T
{
OwnedSlice::from_vec(self.into_vec().move_map(f))
}
}
use syntax::util::move_map::MoveMap;

pub trait Folder : Sized {
// Any additions to this trait should happen in form
Expand Down Expand Up @@ -333,7 +306,7 @@ pub fn noop_fold_view_path<T: Folder>(view_path: P<ViewPath>, fld: &mut T) -> P<
}

pub fn fold_attrs<T: Folder>(attrs: Vec<Attribute>, fld: &mut T) -> Vec<Attribute> {
attrs.into_iter().flat_map(|x| fld.fold_attribute(x)).collect()
attrs.move_flat_map(|x| fld.fold_attribute(x))
}

pub fn noop_fold_arm<T: Folder>(Arm { attrs, pats, guard, body }: Arm, fld: &mut T) -> Arm {
Expand Down Expand Up @@ -771,7 +744,7 @@ pub fn noop_fold_block<T: Folder>(b: P<Block>, folder: &mut T) -> P<Block> {
b.map(|Block { id, stmts, expr, rules, span }| {
Block {
id: folder.new_id(id),
stmts: stmts.into_iter().map(|s| folder.fold_stmt(s)).collect(),
stmts: stmts.move_map(|s| folder.fold_stmt(s)),
expr: expr.map(|x| folder.fold_expr(x)),
rules: rules,
span: folder.new_span(span),
Expand Down Expand Up @@ -818,9 +791,8 @@ pub fn noop_fold_item_underscore<T: Folder>(i: Item_, folder: &mut T) -> Item_ {
ItemDefaultImpl(unsafety, folder.fold_trait_ref((*trait_ref).clone()))
}
ItemImpl(unsafety, polarity, generics, ifce, ty, impl_items) => {
let new_impl_items = impl_items.into_iter()
.map(|item| folder.fold_impl_item(item))
.collect();
let new_impl_items = impl_items
.move_map(|item| folder.fold_impl_item(item));
let ifce = match ifce {
None => None,
Some(ref trait_ref) => {
Expand All @@ -836,9 +808,7 @@ pub fn noop_fold_item_underscore<T: Folder>(i: Item_, folder: &mut T) -> Item_ {
}
ItemTrait(unsafety, generics, bounds, items) => {
let bounds = folder.fold_bounds(bounds);
let items = items.into_iter()
.map(|item| folder.fold_trait_item(item))
.collect();
let items = items.move_map(|item| folder.fold_trait_item(item));
ItemTrait(unsafety, folder.fold_generics(generics), bounds, items)
}
}
Expand Down Expand Up @@ -894,7 +864,7 @@ pub fn noop_fold_impl_item<T: Folder>(i: P<ImplItem>, folder: &mut T) -> P<ImplI
pub fn noop_fold_mod<T: Folder>(Mod { inner, item_ids }: Mod, folder: &mut T) -> Mod {
Mod {
inner: folder.new_span(inner),
item_ids: item_ids.into_iter().map(|x| folder.fold_item_id(x)).collect(),
items_ids: item_ids.move_map(|x| folder.fold_item_id(x)),
}
}

Expand Down
1 change: 0 additions & 1 deletion src/librustc_front/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
#![feature(slice_patterns)]
#![feature(staged_api)]
#![feature(str_char)]
#![feature(filling_drop)]

extern crate serialize;
#[macro_use]
Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/ext/deriving/generic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ use ext::build::AstBuilder;
use codemap::{self, DUMMY_SP};
use codemap::Span;
use diagnostic::SpanHandler;
use fold::MoveMap;
use util::move_map::MoveMap;
use owned_slice::OwnedSlice;
use parse::token::{intern, InternedString};
use parse::token::special_idents;
Expand Down
1 change: 1 addition & 0 deletions src/libsyntax/ext/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use ext::base::*;
use feature_gate::{self, Features, GatedCfg};
use fold;
use fold::*;
use util::move_map::MoveMap;
use parse;
use parse::token::{fresh_mark, fresh_name, intern};
use ptr::P;
Expand Down
48 changes: 13 additions & 35 deletions src/libsyntax/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,34 +26,11 @@ use codemap::{respan, Span, Spanned};
use owned_slice::OwnedSlice;
use parse::token;
use ptr::P;
use std::ptr;
use util::small_vector::SmallVector;
use util::move_map::MoveMap;

use std::rc::Rc;

// This could have a better place to live.
pub trait MoveMap<T> {
fn move_map<F>(self, f: F) -> Self where F: FnMut(T) -> T;
}

impl<T> MoveMap<T> for Vec<T> {
fn move_map<F>(mut self, mut f: F) -> Vec<T> where F: FnMut(T) -> T {
for p in &mut self {
unsafe {
// FIXME(#5016) this shouldn't need to zero to be safe.
ptr::write(p, f(ptr::read_and_drop(p)));
}
}
self
}
}

impl<T> MoveMap<T> for OwnedSlice<T> {
fn move_map<F>(self, f: F) -> OwnedSlice<T> where F: FnMut(T) -> T {
OwnedSlice::from_vec(self.into_vec().move_map(f))
}
}

pub trait Folder : Sized {
// Any additions to this trait should happen in form
// of a call to a public `noop_*` function that only calls
Expand Down Expand Up @@ -362,7 +339,7 @@ pub fn noop_fold_view_path<T: Folder>(view_path: P<ViewPath>, fld: &mut T) -> P<
}

pub fn fold_attrs<T: Folder>(attrs: Vec<Attribute>, fld: &mut T) -> Vec<Attribute> {
attrs.into_iter().flat_map(|x| fld.fold_attribute(x)).collect()
attrs.move_flat_map(|x| fld.fold_attribute(x))
}

pub fn fold_thin_attrs<T: Folder>(attrs: ThinAttributes, fld: &mut T) -> ThinAttributes {
Expand Down Expand Up @@ -623,6 +600,8 @@ pub fn noop_fold_tt<T: Folder>(tt: &TokenTree, fld: &mut T) -> TokenTree {
}

pub fn noop_fold_tts<T: Folder>(tts: &[TokenTree], fld: &mut T) -> Vec<TokenTree> {
// FIXME: Does this have to take a tts slice?
// Could use move_map otherwise...
tts.iter().map(|tt| fld.fold_tt(tt)).collect()
}

Expand Down Expand Up @@ -904,7 +883,7 @@ fn noop_fold_bounds<T: Folder>(bounds: TyParamBounds, folder: &mut T)
pub fn noop_fold_block<T: Folder>(b: P<Block>, folder: &mut T) -> P<Block> {
b.map(|Block {id, stmts, expr, rules, span}| Block {
id: folder.new_id(id),
stmts: stmts.into_iter().flat_map(|s| folder.fold_stmt(s).into_iter()).collect(),
stmts: stmts.move_flat_map(|s| folder.fold_stmt(s).into_iter()),
expr: expr.and_then(|x| folder.fold_opt_expr(x)),
rules: rules,
span: folder.new_span(span),
Expand Down Expand Up @@ -953,9 +932,9 @@ pub fn noop_fold_item_underscore<T: Folder>(i: Item_, folder: &mut T) -> Item_ {
ItemDefaultImpl(unsafety, folder.fold_trait_ref((*trait_ref).clone()))
}
ItemImpl(unsafety, polarity, generics, ifce, ty, impl_items) => {
let new_impl_items = impl_items.into_iter().flat_map(|item| {
folder.fold_impl_item(item).into_iter()
}).collect();
let new_impl_items = impl_items.move_flat_map(|item| {
folder.fold_impl_item(item)
});
let ifce = match ifce {
None => None,
Some(ref trait_ref) => {
Expand All @@ -971,9 +950,9 @@ pub fn noop_fold_item_underscore<T: Folder>(i: Item_, folder: &mut T) -> Item_ {
}
ItemTrait(unsafety, generics, bounds, items) => {
let bounds = folder.fold_bounds(bounds);
let items = items.into_iter().flat_map(|item| {
folder.fold_trait_item(item).into_iter()
}).collect();
let items = items.move_flat_map(|item| {
folder.fold_trait_item(item)
});
ItemTrait(unsafety,
folder.fold_generics(generics),
bounds,
Expand Down Expand Up @@ -1032,7 +1011,7 @@ pub fn noop_fold_impl_item<T: Folder>(i: P<ImplItem>, folder: &mut T)
pub fn noop_fold_mod<T: Folder>(Mod {inner, items}: Mod, folder: &mut T) -> Mod {
Mod {
inner: folder.new_span(inner),
items: items.into_iter().flat_map(|x| folder.fold_item(x).into_iter()).collect(),
items: items.move_flat_map(|x| folder.fold_item(x)),
}
}

Expand Down Expand Up @@ -1353,8 +1332,7 @@ pub fn noop_fold_opt_expr<T: Folder>(e: P<Expr>, folder: &mut T) -> Option<P<Exp
}

pub fn noop_fold_exprs<T: Folder>(es: Vec<P<Expr>>, folder: &mut T) -> Vec<P<Expr>> {
// FIXME: Needs a efficient in-place flat_map
es.into_iter().flat_map(|e| folder.fold_opt_expr(e)).collect()
es.move_flat_map(|e| folder.fold_opt_expr(e))
}

pub fn noop_fold_stmt<T: Folder>(Spanned {node, span}: Stmt, folder: &mut T)
Expand Down
1 change: 1 addition & 0 deletions src/libsyntax/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ pub mod util {
#[cfg(test)]
pub mod parser_testing;
pub mod small_vector;
pub mod move_map;
}

pub mod diagnostics {
Expand Down
2 changes: 0 additions & 2 deletions src/libsyntax/print/pprust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1653,8 +1653,6 @@ impl<'a> State<'a> {
}
if parse::classify::stmt_ends_with_semi(&st.node) {
try!(word(&mut self.s, ";"));
} else {
//try!(word(&mut self.s, ""));
}
self.maybe_print_trailing_comment(st.span, None)
}
Expand Down
3 changes: 2 additions & 1 deletion src/libsyntax/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ use entry::{self, EntryPointType};
use ext::base::ExtCtxt;
use ext::build::AstBuilder;
use ext::expand::ExpansionConfig;
use fold::{Folder, MoveMap};
use fold::Folder;
use util::move_map::MoveMap;
use fold;
use owned_slice::OwnedSlice;
use parse::token::{intern, InternedString};
Expand Down
79 changes: 79 additions & 0 deletions src/libsyntax/util/move_map.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use owned_slice::OwnedSlice;

use std::ptr;

pub trait MoveMap<T>: Sized {
fn move_map<F>(self, mut f: F) -> Self where F: FnMut(T) -> T {
self.move_flat_map(|e| Some(f(e)))
}

fn move_flat_map<F, I>(self, f: F) -> Self
where F: FnMut(T) -> I,
I: IntoIterator<Item=T>;
}

impl<T> MoveMap<T> for Vec<T> {
fn move_flat_map<F, I>(mut self, mut f: F) -> Self
where F: FnMut(T) -> I,
I: IntoIterator<Item=T>
{
let mut read_i = 0;
let mut write_i = 0;
unsafe {
let mut old_len = self.len();
self.set_len(0); // make sure we just leak elements in case of panic

while read_i < old_len {
// move the read_i'th item out of the vector and map it
// to an iterator
let e = ptr::read(self.get_unchecked(read_i));
let mut iter = f(e).into_iter();
read_i += 1;

while let Some(e) = iter.next() {
if write_i < read_i {
ptr::write(self.get_unchecked_mut(write_i), e);
write_i += 1;
} else {
// If this is reached we ran out of space
// in the middle of the vector.
// However, the vector is in a valid state here,
// so we just do a somewhat inefficient insert.
self.set_len(old_len);
self.insert(write_i, e);

old_len = self.len();
self.set_len(0);

read_i += 1;
write_i += 1;
}
}
}

// write_i tracks the number of actually written new items.
self.set_len(write_i);
}

self
}
}

impl<T> MoveMap<T> for OwnedSlice<T> {
fn move_flat_map<F, I>(self, f: F) -> Self
where F: FnMut(T) -> I,
I: IntoIterator<Item=T>
{
OwnedSlice::from_vec(self.into_vec().move_flat_map(f))
}
}
40 changes: 23 additions & 17 deletions src/libsyntax/util/small_vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use std::mem;
use std::slice;
use std::vec;

use fold::MoveMap;
use util::move_map::MoveMap;

/// A vector type optimized for cases where the size is almost always 0 or 1
pub struct SmallVector<T> {
Expand Down Expand Up @@ -134,15 +134,6 @@ impl<T> SmallVector<T> {
self.into_iter()
}

pub fn into_iter(self) -> IntoIter<T> {
let repr = match self.repr {
Zero => ZeroIterator,
One(v) => OneIterator(v),
Many(vs) => ManyIterator(vs.into_iter())
};
IntoIter { repr: repr }
}

pub fn len(&self) -> usize {
match self.repr {
Zero => 0,
Expand All @@ -154,6 +145,19 @@ impl<T> SmallVector<T> {
pub fn is_empty(&self) -> bool { self.len() == 0 }
}

impl<T> IntoIterator for SmallVector<T> {
type Item = T;
type IntoIter = IntoIter<T>;
fn into_iter(self) -> Self::IntoIter {
let repr = match self.repr {
Zero => ZeroIterator,
One(v) => OneIterator(v),
Many(vs) => ManyIterator(vs.into_iter())
};
IntoIter { repr: repr }
}
}

pub struct IntoIter<T> {
repr: IntoIterRepr<T>,
}
Expand Down Expand Up @@ -192,13 +196,15 @@ impl<T> Iterator for IntoIter<T> {
}

impl<T> MoveMap<T> for SmallVector<T> {
fn move_map<F>(self, mut f: F) -> SmallVector<T> where F: FnMut(T) -> T {
let repr = match self.repr {
Zero => Zero,
One(v) => One(f(v)),
Many(vs) => Many(vs.move_map(f))
};
SmallVector { repr: repr }
fn move_flat_map<F, I>(self, mut f: F) -> Self
where F: FnMut(T) -> I,
I: IntoIterator<Item=T>
{
match self.repr {
Zero => Self::zero(),
One(v) => f(v).into_iter().collect(),
Many(vs) => SmallVector { repr: Many(vs.move_flat_map(f)) },
}
}
}

Expand Down

0 comments on commit c56b47a

Please sign in to comment.