Skip to content

Commit d87611d

Browse files
committed
---
yaml --- r: 151404 b: refs/heads/try2 c: e0d43b0 h: refs/heads/master v: v3
1 parent 1fd1edd commit d87611d

File tree

3 files changed

+201
-1
lines changed

3 files changed

+201
-1
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ refs/heads/snap-stage3: 78a7676898d9f80ab540c6df5d4c9ce35bb50463
55
refs/heads/try: 519addf6277dbafccbb4159db4b710c37eaa2ec5
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
8-
refs/heads/try2: c67ebf1ef3fa7766ad17059fc953095116eadba9
8+
refs/heads/try2: e0d43b023ed219d47c0785f10c62a62350aa4ea1
99
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/try2/src/libcore/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,6 @@ pub mod char;
7373
pub mod slice;
7474
pub mod str;
7575
pub mod tuple;
76+
77+
// FIXME: this module should not exist
78+
mod should_not_exist;
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use cast;
12+
use char::Char;
13+
use clone::Clone;
14+
use container::Container;
15+
use default::Default;
16+
use intrinsics;
17+
use iter::{Iterator, FromIterator};
18+
use mem;
19+
use num::{CheckedMul, CheckedAdd};
20+
use ops::Add;
21+
use option::{Some, None};
22+
use ptr::RawPtr;
23+
use ptr;
24+
use raw::Vec;
25+
use slice::{ImmutableVector, Vector};
26+
use str::StrSlice;
27+
28+
#[allow(ctypes)]
29+
extern {
30+
fn malloc(size: uint) -> *u8;
31+
fn free(ptr: *u8);
32+
}
33+
34+
unsafe fn alloc(cap: uint) -> *mut Vec<()> {
35+
let cap = cap.checked_add(&mem::size_of::<Vec<()>>()).expect("cap overflow");
36+
let ret = malloc(cap) as *mut Vec<()>;
37+
if ret.is_null() {
38+
intrinsics::abort();
39+
}
40+
(*ret).fill = 0;
41+
(*ret).alloc = cap;
42+
ret
43+
}
44+
45+
// Strings
46+
47+
impl Default for ~str {
48+
fn default() -> ~str {
49+
unsafe {
50+
// Get some memory
51+
let ptr = alloc(0);
52+
53+
// Initialize the memory
54+
(*ptr).fill = 0;
55+
(*ptr).alloc = 0;
56+
57+
cast::transmute(ptr)
58+
}
59+
}
60+
}
61+
62+
impl Clone for ~str {
63+
fn clone(&self) -> ~str {
64+
// Don't use the clone() implementation above because it'll start
65+
// requiring the eh_personality lang item (no fun)
66+
unsafe {
67+
let bytes = self.as_bytes().as_ptr();
68+
let len = self.len();
69+
70+
let ptr = alloc(len) as *mut Vec<u8>;
71+
ptr::copy_nonoverlapping_memory(&mut (*ptr).data, bytes, len);
72+
(*ptr).fill = len;
73+
(*ptr).alloc = len;
74+
75+
cast::transmute(ptr)
76+
}
77+
}
78+
}
79+
80+
impl FromIterator<char> for ~str {
81+
#[inline]
82+
fn from_iter<T: Iterator<char>>(mut iterator: T) -> ~str {
83+
let (lower, _) = iterator.size_hint();
84+
let mut cap = if lower == 0 {16} else {lower};
85+
let mut len = 0;
86+
let mut tmp = [0u8, ..4];
87+
88+
unsafe {
89+
let mut ptr = alloc(cap) as *mut Vec<u8>;
90+
let mut ret = cast::transmute(ptr);
91+
for ch in iterator {
92+
let amt = ch.encode_utf8(tmp);
93+
94+
if len + amt > cap {
95+
cap = cap.checked_mul(&2).expect("cap overflow");
96+
if cap < len + amt {
97+
cap = len + amt;
98+
}
99+
let ptr2 = alloc(cap) as *mut Vec<u8>;
100+
ptr::copy_nonoverlapping_memory(&mut (*ptr2).data,
101+
&(*ptr).data,
102+
len);
103+
free(ptr as *u8);
104+
cast::forget(ret);
105+
ret = cast::transmute(ptr2);
106+
ptr = ptr2;
107+
}
108+
109+
let base = &mut (*ptr).data as *mut u8;
110+
for byte in tmp.slice_to(amt).iter() {
111+
*base.offset(len as int) = *byte;
112+
len += 1;
113+
}
114+
(*ptr).fill = len;
115+
}
116+
ret
117+
}
118+
}
119+
}
120+
121+
impl<'a> Add<&'a str,~str> for &'a str {
122+
#[inline]
123+
fn add(&self, rhs: & &'a str) -> ~str {
124+
let amt = self.len().checked_add(&rhs.len()).expect("len overflow");
125+
unsafe {
126+
let ptr = alloc(amt) as *mut Vec<u8>;
127+
let base = &mut (*ptr).data as *mut _;
128+
ptr::copy_nonoverlapping_memory(base,
129+
self.as_bytes().as_ptr(),
130+
self.len());
131+
let base = base.offset(self.len() as int);
132+
ptr::copy_nonoverlapping_memory(base,
133+
rhs.as_bytes().as_ptr(),
134+
rhs.len());
135+
(*ptr).fill = amt;
136+
(*ptr).alloc = amt;
137+
cast::transmute(ptr)
138+
}
139+
}
140+
}
141+
142+
// Arrays
143+
144+
impl<A: Clone> Clone for ~[A] {
145+
#[inline]
146+
fn clone(&self) -> ~[A] {
147+
self.iter().map(|a| a.clone()).collect()
148+
}
149+
}
150+
151+
impl<A> FromIterator<A> for ~[A] {
152+
fn from_iter<T: Iterator<A>>(mut iterator: T) -> ~[A] {
153+
let (lower, _) = iterator.size_hint();
154+
let cap = if lower == 0 {16} else {lower};
155+
let mut cap = cap.checked_mul(&mem::size_of::<A>()).expect("cap overflow");
156+
let mut len = 0;
157+
158+
unsafe {
159+
let mut ptr = alloc(cap) as *mut Vec<A>;
160+
let mut ret = cast::transmute(ptr);
161+
for elt in iterator {
162+
if len * mem::size_of::<A>() >= cap {
163+
cap = cap.checked_mul(&2).expect("cap overflow");
164+
let ptr2 = alloc(cap) as *mut Vec<A>;
165+
ptr::copy_nonoverlapping_memory(&mut (*ptr2).data,
166+
&(*ptr).data,
167+
len);
168+
free(ptr as *u8);
169+
cast::forget(ret);
170+
ret = cast::transmute(ptr2);
171+
ptr = ptr2;
172+
}
173+
174+
let base = &mut (*ptr).data as *mut A;
175+
intrinsics::move_val_init(&mut *base.offset(len as int), elt);
176+
len += 1;
177+
(*ptr).fill = len * mem::nonzero_size_of::<A>();
178+
}
179+
ret
180+
}
181+
}
182+
}
183+
184+
impl<'a,T:Clone, V: Vector<T>> Add<V, ~[T]> for &'a [T] {
185+
#[inline]
186+
fn add(&self, rhs: &V) -> ~[T] {
187+
let first = self.iter().map(|t| t.clone());
188+
first.chain(rhs.as_slice().iter().map(|t| t.clone())).collect()
189+
}
190+
}
191+
192+
impl<T:Clone, V: Vector<T>> Add<V, ~[T]> for ~[T] {
193+
#[inline]
194+
fn add(&self, rhs: &V) -> ~[T] {
195+
self.as_slice() + rhs.as_slice()
196+
}
197+
}

0 commit comments

Comments
 (0)