提交 6267d8a9 编写于 作者: B Brian Anderson

core: De-export at_vec and extfmt

上级 f3d6c506
...@@ -2,34 +2,24 @@ ...@@ -2,34 +2,24 @@
use ptr::addr_of; use ptr::addr_of;
export init_op;
export capacity;
export build_sized, build, build_sized_opt;
export map;
export from_fn, from_elem;
export raw;
export traits;
/// Code for dealing with @-vectors. This is pretty incomplete, and /// Code for dealing with @-vectors. This is pretty incomplete, and
/// contains a bunch of duplication from the code for ~-vectors. /// contains a bunch of duplication from the code for ~-vectors.
#[abi = "cdecl"] #[abi = "cdecl"]
extern mod rustrt { extern mod rustrt {
#[legacy_exports]; pub fn vec_reserve_shared_actual(++t: *sys::TypeDesc,
fn vec_reserve_shared_actual(++t: *sys::TypeDesc,
++v: **vec::raw::VecRepr, ++v: **vec::raw::VecRepr,
++n: libc::size_t); ++n: libc::size_t);
} }
#[abi = "rust-intrinsic"] #[abi = "rust-intrinsic"]
extern mod rusti { extern mod rusti {
#[legacy_exports]; pub fn move_val_init<T>(&dst: T, -src: T);
fn move_val_init<T>(&dst: T, -src: T);
} }
/// Returns the number of elements the vector can hold without reallocating /// Returns the number of elements the vector can hold without reallocating
#[inline(always)] #[inline(always)]
pure fn capacity<T>(&&v: @[const T]) -> uint { pub pure fn capacity<T>(&&v: @[const T]) -> uint {
unsafe { unsafe {
let repr: **raw::VecRepr = let repr: **raw::VecRepr =
::cast::reinterpret_cast(&addr_of(v)); ::cast::reinterpret_cast(&addr_of(v));
...@@ -50,7 +40,8 @@ fn vec_reserve_shared_actual(++t: *sys::TypeDesc, ...@@ -50,7 +40,8 @@ fn vec_reserve_shared_actual(++t: *sys::TypeDesc,
* onto the vector being constructed. * onto the vector being constructed.
*/ */
#[inline(always)] #[inline(always)]
pure fn build_sized<A>(size: uint, builder: fn(push: pure fn(+A))) -> @[A] { pub pure fn build_sized<A>(size: uint,
builder: fn(push: pure fn(+A))) -> @[A] {
let mut vec = @[]; let mut vec = @[];
unsafe { raw::reserve(vec, size); } unsafe { raw::reserve(vec, size); }
builder(|+x| unsafe { raw::push(vec, move x) }); builder(|+x| unsafe { raw::push(vec, move x) });
...@@ -68,7 +59,7 @@ fn vec_reserve_shared_actual(++t: *sys::TypeDesc, ...@@ -68,7 +59,7 @@ fn vec_reserve_shared_actual(++t: *sys::TypeDesc,
* onto the vector being constructed. * onto the vector being constructed.
*/ */
#[inline(always)] #[inline(always)]
pure fn build<A>(builder: fn(push: pure fn(+A))) -> @[A] { pub pure fn build<A>(builder: fn(push: pure fn(+A))) -> @[A] {
build_sized(4, builder) build_sized(4, builder)
} }
...@@ -85,7 +76,7 @@ fn vec_reserve_shared_actual(++t: *sys::TypeDesc, ...@@ -85,7 +76,7 @@ fn vec_reserve_shared_actual(++t: *sys::TypeDesc,
* onto the vector being constructed. * onto the vector being constructed.
*/ */
#[inline(always)] #[inline(always)]
pure fn build_sized_opt<A>(size: Option<uint>, pub pure fn build_sized_opt<A>(size: Option<uint>,
builder: fn(push: pure fn(+A))) -> @[A] { builder: fn(push: pure fn(+A))) -> @[A] {
build_sized(size.get_default(4), builder) build_sized(size.get_default(4), builder)
} }
...@@ -101,7 +92,7 @@ fn vec_reserve_shared_actual(++t: *sys::TypeDesc, ...@@ -101,7 +92,7 @@ fn vec_reserve_shared_actual(++t: *sys::TypeDesc,
/// Apply a function to each element of a vector and return the results /// Apply a function to each element of a vector and return the results
pure fn map<T, U>(v: &[T], f: fn(T) -> U) -> @[U] { pub pure fn map<T, U>(v: &[T], f: fn(T) -> U) -> @[U] {
do build_sized(v.len()) |push| { do build_sized(v.len()) |push| {
for vec::each(v) |elem| { for vec::each(v) |elem| {
push(f(*elem)); push(f(*elem));
...@@ -115,7 +106,7 @@ fn vec_reserve_shared_actual(++t: *sys::TypeDesc, ...@@ -115,7 +106,7 @@ fn vec_reserve_shared_actual(++t: *sys::TypeDesc,
* Creates an immutable vector of size `n_elts` and initializes the elements * Creates an immutable vector of size `n_elts` and initializes the elements
* to the value returned by the function `op`. * to the value returned by the function `op`.
*/ */
pure fn from_fn<T>(n_elts: uint, op: iter::InitOp<T>) -> @[T] { pub pure fn from_fn<T>(n_elts: uint, op: iter::InitOp<T>) -> @[T] {
do build_sized(n_elts) |push| { do build_sized(n_elts) |push| {
let mut i: uint = 0u; let mut i: uint = 0u;
while i < n_elts { push(op(i)); i += 1u; } while i < n_elts { push(op(i)); i += 1u; }
...@@ -128,7 +119,7 @@ fn vec_reserve_shared_actual(++t: *sys::TypeDesc, ...@@ -128,7 +119,7 @@ fn vec_reserve_shared_actual(++t: *sys::TypeDesc,
* Creates an immutable vector of size `n_elts` and initializes the elements * Creates an immutable vector of size `n_elts` and initializes the elements
* to the value `t`. * to the value `t`.
*/ */
pure fn from_elem<T: Copy>(n_elts: uint, t: T) -> @[T] { pub pure fn from_elem<T: Copy>(n_elts: uint, t: T) -> @[T] {
do build_sized(n_elts) |push| { do build_sized(n_elts) |push| {
let mut i: uint = 0u; let mut i: uint = 0u;
while i < n_elts { push(t); i += 1u; } while i < n_elts { push(t); i += 1u; }
...@@ -137,7 +128,6 @@ fn vec_reserve_shared_actual(++t: *sys::TypeDesc, ...@@ -137,7 +128,6 @@ fn vec_reserve_shared_actual(++t: *sys::TypeDesc,
#[cfg(notest)] #[cfg(notest)]
mod traits { mod traits {
#[legacy_exports];
#[cfg(stage0)] #[cfg(stage0)]
impl<T: Copy> @[T]: Add<&[const T],@[T]> { impl<T: Copy> @[T]: Add<&[const T],@[T]> {
#[inline(always)] #[inline(always)]
...@@ -159,10 +149,9 @@ impl<T: Copy> @[T] : Add<&[const T],@[T]> { ...@@ -159,10 +149,9 @@ impl<T: Copy> @[T] : Add<&[const T],@[T]> {
mod traits { mod traits {
#[legacy_exports];} #[legacy_exports];}
mod raw { pub mod raw {
#[legacy_exports]; pub type VecRepr = vec::raw::VecRepr;
type VecRepr = vec::raw::VecRepr; pub type SliceRepr = vec::raw::SliceRepr;
type SliceRepr = vec::raw::SliceRepr;
/** /**
* Sets the length of a vector * Sets the length of a vector
...@@ -172,13 +161,13 @@ mod raw { ...@@ -172,13 +161,13 @@ mod raw {
* the vector is actually the specified size. * the vector is actually the specified size.
*/ */
#[inline(always)] #[inline(always)]
unsafe fn set_len<T>(&&v: @[const T], new_len: uint) { pub unsafe fn set_len<T>(&&v: @[const T], new_len: uint) {
let repr: **VecRepr = ::cast::reinterpret_cast(&addr_of(v)); let repr: **VecRepr = ::cast::reinterpret_cast(&addr_of(v));
(**repr).unboxed.fill = new_len * sys::size_of::<T>(); (**repr).unboxed.fill = new_len * sys::size_of::<T>();
} }
#[inline(always)] #[inline(always)]
unsafe fn push<T>(&v: @[const T], +initval: T) { pub unsafe fn push<T>(&v: @[const T], +initval: T) {
let repr: **VecRepr = ::cast::reinterpret_cast(&addr_of(v)); let repr: **VecRepr = ::cast::reinterpret_cast(&addr_of(v));
let fill = (**repr).unboxed.fill; let fill = (**repr).unboxed.fill;
if (**repr).unboxed.alloc > fill { if (**repr).unboxed.alloc > fill {
...@@ -215,7 +204,7 @@ unsafe fn push_slow<T>(&v: @[const T], +initval: T) { ...@@ -215,7 +204,7 @@ unsafe fn push_slow<T>(&v: @[const T], +initval: T) {
* * v - A vector * * v - A vector
* * n - The number of elements to reserve space for * * n - The number of elements to reserve space for
*/ */
unsafe fn reserve<T>(&v: @[const T], n: uint) { pub unsafe fn reserve<T>(&v: @[const T], n: uint) {
// Only make the (slow) call into the runtime if we have to // Only make the (slow) call into the runtime if we have to
if capacity(v) < n { if capacity(v) < n {
let ptr = addr_of(v) as **VecRepr; let ptr = addr_of(v) as **VecRepr;
...@@ -239,7 +228,7 @@ unsafe fn reserve<T>(&v: @[const T], n: uint) { ...@@ -239,7 +228,7 @@ unsafe fn reserve<T>(&v: @[const T], n: uint) {
* * v - A vector * * v - A vector
* * n - The number of elements to reserve space for * * n - The number of elements to reserve space for
*/ */
unsafe fn reserve_at_least<T>(&v: @[const T], n: uint) { pub unsafe fn reserve_at_least<T>(&v: @[const T], n: uint) {
reserve(v, uint::next_power_of_two(n)); reserve(v, uint::next_power_of_two(n));
} }
......
...@@ -41,37 +41,36 @@ ...@@ -41,37 +41,36 @@
*/ */
// Functions used by the fmt extension at compile time // Functions used by the fmt extension at compile time
mod ct { pub mod ct {
#[legacy_exports]; pub enum Signedness { pub Signed, pub Unsigned, }
enum Signedness { Signed, Unsigned, } pub enum Caseness { pub CaseUpper, pub CaseLower, }
enum Caseness { CaseUpper, CaseLower, } pub enum Ty {
enum Ty { pub TyBool,
TyBool, pub TyStr,
TyStr, pub TyChar,
TyChar, pub TyInt(Signedness),
TyInt(Signedness), pub TyBits,
TyBits, pub TyHex(Caseness),
TyHex(Caseness), pub TyOctal,
TyOctal, pub TyFloat,
TyFloat, pub TyPoly,
TyPoly, }
} pub enum Flag {
enum Flag { pub FlagLeftJustify,
FlagLeftJustify, pub FlagLeftZeroPad,
FlagLeftZeroPad, pub FlagSpaceForSign,
FlagSpaceForSign, pub FlagSignAlways,
FlagSignAlways, pub FlagAlternate,
FlagAlternate, }
} pub enum Count {
enum Count { pub CountIs(int),
CountIs(int), pub CountIsParam(int),
CountIsParam(int), pub CountIsNextParam,
CountIsNextParam, pub CountImplied,
CountImplied,
} }
// A formatted conversion from an expression to a string // A formatted conversion from an expression to a string
type Conv = pub type Conv =
{param: Option<int>, {param: Option<int>,
flags: ~[Flag], flags: ~[Flag],
width: Count, width: Count,
...@@ -80,10 +79,10 @@ enum Count { ...@@ -80,10 +79,10 @@ enum Count {
// A fragment of the output sequence // A fragment of the output sequence
enum Piece { PieceString(~str), PieceConv(Conv), } pub enum Piece { PieceString(~str), PieceConv(Conv), }
type ErrorFn = fn@(~str) -> ! ; pub type ErrorFn = fn@(~str) -> ! ;
fn parse_fmt_string(s: ~str, error: ErrorFn) -> ~[Piece] { pub fn parse_fmt_string(s: ~str, error: ErrorFn) -> ~[Piece] {
let mut pieces: ~[Piece] = ~[]; let mut pieces: ~[Piece] = ~[];
let lim = str::len(s); let lim = str::len(s);
let mut buf = ~""; let mut buf = ~"";
...@@ -273,21 +272,26 @@ fn parse_type(s: ~str, i: uint, lim: uint, error: ErrorFn) -> ...@@ -273,21 +272,26 @@ fn parse_type(s: ~str, i: uint, lim: uint, error: ErrorFn) ->
// decisions made a runtime. If it proves worthwhile then some of these // decisions made a runtime. If it proves worthwhile then some of these
// conditions can be evaluated at compile-time. For now though it's cleaner to // conditions can be evaluated at compile-time. For now though it's cleaner to
// implement it 0this way, I think. // implement it 0this way, I think.
mod rt { pub mod rt {
#[legacy_exports]; pub const flag_none: u32 = 0u32;
const flag_none : u32 = 0u32; pub const flag_left_justify : u32 = 0b00000000000000000000000000000001u32;
const flag_left_justify : u32 = 0b00000000000000000000000000000001u32; pub const flag_left_zero_pad: u32 = 0b00000000000000000000000000000010u32;
const flag_left_zero_pad : u32 = 0b00000000000000000000000000000010u32; pub const flag_space_for_sign:u32 = 0b00000000000000000000000000000100u32;
const flag_space_for_sign : u32 = 0b00000000000000000000000000000100u32; pub const flag_sign_always : u32 = 0b00000000000000000000000000001000u32;
const flag_sign_always : u32 = 0b00000000000000000000000000001000u32; pub const flag_alternate : u32 = 0b00000000000000000000000000010000u32;
const flag_alternate : u32 = 0b00000000000000000000000000010000u32;
pub enum Count { pub CountIs(int), pub CountImplied, }
enum Count { CountIs(int), CountImplied, } pub enum Ty {
enum Ty { TyDefault, TyBits, TyHexUpper, TyHexLower, TyOctal, } pub TyDefault,
pub TyBits,
type Conv = {flags: u32, width: Count, precision: Count, ty: Ty}; pub TyHexUpper,
pub TyHexLower,
pure fn conv_int(cv: Conv, i: int) -> ~str { pub TyOctal
}
pub type Conv = {flags: u32, width: Count, precision: Count, ty: Ty};
pub pure fn conv_int(cv: Conv, i: int) -> ~str {
let radix = 10u; let radix = 10u;
let prec = get_int_precision(cv); let prec = get_int_precision(cv);
let mut s : ~str = int_to_str_prec(i, radix, prec); let mut s : ~str = int_to_str_prec(i, radix, prec);
...@@ -300,7 +304,7 @@ enum Ty { TyDefault, TyBits, TyHexUpper, TyHexLower, TyOctal, } ...@@ -300,7 +304,7 @@ enum Ty { TyDefault, TyBits, TyHexUpper, TyHexLower, TyOctal, }
} }
return unsafe { pad(cv, s, PadSigned) }; return unsafe { pad(cv, s, PadSigned) };
} }
pure fn conv_uint(cv: Conv, u: uint) -> ~str { pub pure fn conv_uint(cv: Conv, u: uint) -> ~str {
let prec = get_int_precision(cv); let prec = get_int_precision(cv);
let mut rs = let mut rs =
match cv.ty { match cv.ty {
...@@ -312,17 +316,17 @@ enum Ty { TyDefault, TyBits, TyHexUpper, TyHexLower, TyOctal, } ...@@ -312,17 +316,17 @@ enum Ty { TyDefault, TyBits, TyHexUpper, TyHexLower, TyOctal, }
}; };
return unsafe { pad(cv, rs, PadUnsigned) }; return unsafe { pad(cv, rs, PadUnsigned) };
} }
pure fn conv_bool(cv: Conv, b: bool) -> ~str { pub pure fn conv_bool(cv: Conv, b: bool) -> ~str {
let s = if b { ~"true" } else { ~"false" }; let s = if b { ~"true" } else { ~"false" };
// run the boolean conversion through the string conversion logic, // run the boolean conversion through the string conversion logic,
// giving it the same rules for precision, etc. // giving it the same rules for precision, etc.
return conv_str(cv, s); return conv_str(cv, s);
} }
pure fn conv_char(cv: Conv, c: char) -> ~str { pub pure fn conv_char(cv: Conv, c: char) -> ~str {
let mut s = str::from_char(c); let mut s = str::from_char(c);
return unsafe { pad(cv, s, PadNozero) }; return unsafe { pad(cv, s, PadNozero) };
} }
pure fn conv_str(cv: Conv, s: &str) -> ~str { pub pure fn conv_str(cv: Conv, s: &str) -> ~str {
// For strings, precision is the maximum characters // For strings, precision is the maximum characters
// displayed // displayed
let mut unpadded = match cv.precision { let mut unpadded = match cv.precision {
...@@ -335,7 +339,7 @@ enum Ty { TyDefault, TyBits, TyHexUpper, TyHexLower, TyOctal, } ...@@ -335,7 +339,7 @@ enum Ty { TyDefault, TyBits, TyHexUpper, TyHexLower, TyOctal, }
}; };
return unsafe { pad(cv, unpadded, PadNozero) }; return unsafe { pad(cv, unpadded, PadNozero) };
} }
pure fn conv_float(cv: Conv, f: float) -> ~str { pub pure fn conv_float(cv: Conv, f: float) -> ~str {
let (to_str, digits) = match cv.precision { let (to_str, digits) = match cv.precision {
CountIs(c) => (float::to_str_exact, c as uint), CountIs(c) => (float::to_str_exact, c as uint),
CountImplied => (float::to_str, 6u) CountImplied => (float::to_str, 6u)
...@@ -350,7 +354,7 @@ enum Ty { TyDefault, TyBits, TyHexUpper, TyHexLower, TyOctal, } ...@@ -350,7 +354,7 @@ enum Ty { TyDefault, TyBits, TyHexUpper, TyHexLower, TyOctal, }
} }
return unsafe { pad(cv, s, PadFloat) }; return unsafe { pad(cv, s, PadFloat) };
} }
pure fn conv_poly<T>(cv: Conv, v: T) -> ~str { pub pure fn conv_poly<T>(cv: Conv, v: T) -> ~str {
let s = sys::log_str(&v); let s = sys::log_str(&v);
return conv_str(cv, s); return conv_str(cv, s);
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册