Rename last_total to last_unsafe

See Issue 1943 for any discussion (reopen it if necessary).

Closes #1943
This commit is contained in:
Tim Chevalier 2012-03-08 12:08:47 -08:00
parent c9cf73f0a8
commit ebc1d3e704
7 changed files with 21 additions and 11 deletions

View File

@ -169,7 +169,8 @@ fn splitext(p: path) -> (str, str) {
let parts = str::split_char(p, '.');
if vec::len(parts) > 1u {
let base = str::connect(vec::init(parts), ".");
let ext = "." + vec::last_total(parts);
// We just checked that parts is non-empty, so this is safe
let ext = "." + vec::last_unsafe(parts);
fn is_dotfile(base: str) -> bool {
str::is_empty(base)

View File

@ -200,14 +200,15 @@ pure fn last<T: copy>(v: [const T]) -> option<T> {
}
/*
Function: last_total
Function: last_unsafe
Returns the last element of a non-empty vector `v`
Returns the last element of a `v`, failing if the vector is empty.
Predicates:
<is_not_empty> (v)
*/
pure fn last_total<T: copy>(v: [const T]) -> T { v[len(v) - 1u] }
pure fn last_unsafe<T: copy>(v: [const T]) -> T {
if len(v) == 0u { fail "last_unsafe: empty vector" }
v[len(v) - 1u]
}
/*
Function: slice

View File

@ -270,7 +270,8 @@ fn splitext(p: path) -> (str, str) {
let parts = str::split_char(p, '.');
if vec::len(parts) > 1u {
let base = str::connect(vec::init(parts), ".");
let ext = "." + vec::last_total(parts);
// We just checked that parts is non-empty
let ext = "." + vec::last_unsafe(parts);
fn is_dotfile(base: str) -> bool {
str::is_empty(base)

View File

@ -187,7 +187,9 @@ fn map_view_item(vi: @view_item, cx: ctx, _v: vt) {
let (id, name) = alt vp.node {
view_path_simple(nm, _, id) { (id, nm) }
view_path_glob(pth, id) | view_path_list(pth, _, id) {
(id, vec::last_total(*pth))
// should be a constraint on the type
assert (vec::is_not_empty(*pth));
(id, vec::last_unsafe(*pth))
}
};
cx.map.insert(id, node_export(vp, extend(cx, name)));

View File

@ -68,4 +68,7 @@ fn pat_binding_ids(dm: resolve::def_map, pat: @pat) -> [node_id] {
ret found;
}
fn path_to_ident(p: @path) -> ident { vec::last_total(p.node.idents) }
fn path_to_ident(p: @path) -> ident {
assert (vec::is_not_empty(p.node.idents)); // should be a constraint on path
vec::last_unsafe(p.node.idents)
}

View File

@ -161,7 +161,9 @@ fn build_reexport_path_map(srv: astsrv::srv, -def_map: def_map) -> path_map {
let path = alt check ctxt.ast_map.get(exp_id) {
ast_map::node_export(_, path) { path }
};
let name = alt check vec::last_total(*path) {
// should be a constraint on the node_export constructor
// that guarantees path is non-empty
let name = alt check vec::last_unsafe(*path) {
ast_map::path_name(nm) { nm }
};
let modpath = ast_map::path_to_str(vec::init(*path));

View File

@ -19,5 +19,5 @@ fn main() {
check (is_not_empty(ps));
assert (head(ps) == ('a', 1u));
assert (last_total(ps) == (j as char, 10u));
assert (last_unsafe(ps) == (j as char, 10u));
}