Skip to content

Commit afaff7a

Browse files
committed
---
yaml --- r: 149986 b: refs/heads/try2 c: bf67783 h: refs/heads/master v: v3
1 parent 999a5b4 commit afaff7a

File tree

162 files changed

+890
-320
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

162 files changed

+890
-320
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: 1d828eb3a4f63c38231c1fdc0aa2197a9b2a333f
8+
refs/heads/try2: bf67783332ff9f9bdf40da2d9dff860964600420
99
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/try2/mk/crates.mk

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
#
3838
# DEPS_<crate>
3939
# These lists are the dependencies of the <crate> that is to be built.
40-
# Rust dependencies are listed bare (i.e. std, green) and native
40+
# Rust dependencies are listed bare (i.e. std, extra, green) and native
4141
# dependencies have a "native:" prefix (i.e. native:sundown). All deps
4242
# will be built before the crate itself is built.
4343
#
@@ -49,23 +49,23 @@
4949
# automatically generated for all stage/host/target combinations.
5050
################################################################################
5151

52-
TARGET_CRATES := std green rustuv native flate arena glob term semver \
53-
uuid serialize sync getopts collections num test time rand \
54-
workcache url
52+
TARGET_CRATES := std extra green rustuv native flate arena glob term semver \
53+
uuid serialize sync getopts collections num test time rand
5554
HOST_CRATES := syntax rustc rustdoc fourcc hexfloat
5655
CRATES := $(TARGET_CRATES) $(HOST_CRATES)
5756
TOOLS := compiletest rustdoc rustc
5857

5958
DEPS_std := native:rustrt native:compiler-rt native:backtrace
59+
DEPS_extra := std term sync serialize getopts collections time rand
6060
DEPS_green := std rand native:context_switch
6161
DEPS_rustuv := std native:uv native:uv_support
6262
DEPS_native := std
6363
DEPS_syntax := std term serialize collections
6464
DEPS_rustc := syntax native:rustllvm flate arena serialize sync getopts \
65-
collections time
65+
collections time extra
6666
DEPS_rustdoc := rustc native:sundown serialize sync getopts collections \
6767
test time
68-
DEPS_flate := std native:miniz
68+
DEPS_flate := std extra native:miniz
6969
DEPS_arena := std collections
7070
DEPS_glob := std
7171
DEPS_serialize := std collections
@@ -78,11 +78,9 @@ DEPS_collections := std rand
7878
DEPS_fourcc := syntax std
7979
DEPS_hexfloat := syntax std
8080
DEPS_num := std rand
81-
DEPS_test := std collections getopts serialize term time
81+
DEPS_test := std extra collections getopts serialize term
8282
DEPS_time := std serialize
8383
DEPS_rand := std
84-
DEPS_url := std collections
85-
DEPS_workcache := std serialize collections std
8684

8785
TOOL_DEPS_compiletest := test green rustuv getopts
8886
TOOL_DEPS_rustdoc := rustdoc native

branches/try2/src/doc/index.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ li {list-style-type: none; }
5050
* [The `test` library containing the unit-testing & micro-benchmark framework](test/index.html)
5151
* [The `time` library](time/index.html)
5252
* [The `uuid` 128-bit universally unique identifier library](uuid/index.html)
53-
* [The `url` library](url/index.html)
54-
* [The `workcache` library](workcache/index.html)
5553

5654
# Tooling
5755

branches/try2/src/doc/rust.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -787,9 +787,9 @@ Four examples of `extern crate` declarations:
787787
~~~~ {.ignore}
788788
extern crate pcre;
789789
790-
extern crate std; // equivalent to: extern crate std = "std";
790+
extern crate extra; // equivalent to: extern crate extra = "extra";
791791
792-
extern crate ruststd = "std"; // linking to 'std' under another name
792+
extern crate rustextra = "extra"; // linking to 'extra' under another name
793793
794794
extern crate foo = "some/where/rust-foo#foo:1.0"; // a full package ID for external tools
795795
~~~~

branches/try2/src/doc/tutorial.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3228,6 +3228,17 @@ See the [API documentation][stddoc] for details.
32283228

32293229
[stddoc]: std/index.html
32303230

3231+
## The extra library
3232+
3233+
Rust ships with crates such as the [extra library], an accumulation of useful things,
3234+
that are however not important enough to deserve a place in the standard
3235+
library. You can link to a library such as `extra` with an `extern crate extra;`.
3236+
3237+
[extra library]: extra/index.html
3238+
3239+
Right now `extra` contains those definitions directly, but in the future it will likely just
3240+
re-export a bunch of 'officially blessed' crates that get managed with a package manager.
3241+
32313242
# What next?
32323243

32333244
Now that you know the essentials, check out any of the additional

branches/try2/src/etc/combine-tests.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ def scrub(b):
5656
#[feature(globs, macro_rules, struct_variant, managed_boxes)];
5757
#[allow(warnings)];
5858
extern crate collections;
59+
extern crate extra;
5960
"""
6061
)
6162
for t in stage2_tests:
@@ -72,6 +73,7 @@ def scrub(b):
7273
"""
7374
// AUTO-GENERATED FILE: DO NOT EDIT
7475
#[feature(globs, managed_boxes)];
76+
extern crate extra;
7577
extern crate run_pass_stage2;
7678
use run_pass_stage2::*;
7779
use std::io;

branches/try2/src/etc/generate-deriving-span-tests.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
// This file was auto-generated using 'src/etc/generate-keyword-span-tests.py'
3939
4040
#[feature(struct_variant)];
41+
extern crate extra;
4142
extern crate rand;
4243
4344
{error_deriving}

branches/try2/src/libextra/c_vec.rs

Lines changed: 240 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
1+
// Copyright 2012 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+
/*!
12+
* Library to interface with chunks of memory allocated in C.
13+
*
14+
* It is often desirable to safely interface with memory allocated from C,
15+
* encapsulating the unsafety into allocation and destruction time. Indeed,
16+
* allocating memory externally is currently the only way to give Rust shared
17+
* mut state with C programs that keep their own references; vectors are
18+
* unsuitable because they could be reallocated or moved at any time, and
19+
* importing C memory into a vector takes a one-time snapshot of the memory.
20+
*
21+
* This module simplifies the usage of such external blocks of memory. Memory
22+
* is encapsulated into an opaque object after creation; the lifecycle of the
23+
* memory can be optionally managed by Rust, if an appropriate destructor
24+
* closure is provided. Safety is ensured by bounds-checking accesses, which
25+
* are marshalled through get and set functions.
26+
*
27+
* There are three unsafe functions: the two constructors, and the
28+
* unwrap method. The constructors are unsafe for the
29+
* obvious reason (they act on a pointer that cannot be checked inside the
30+
* method), but `unwrap()` is somewhat more subtle in its unsafety.
31+
* It returns the contained pointer, but at the same time destroys the CVec
32+
* without running its destructor. This can be used to pass memory back to
33+
* C, but care must be taken that the ownership of underlying resources are
34+
* handled correctly, i.e. that allocated memory is eventually freed
35+
* if necessary.
36+
*/
37+
38+
use std::cast;
39+
use std::ptr;
40+
use std::raw;
41+
42+
/**
43+
* The type representing a foreign chunk of memory
44+
*/
45+
pub struct CVec<T> {
46+
priv base: *mut T,
47+
priv len: uint,
48+
priv rsrc: DtorRes,
49+
}
50+
51+
struct DtorRes {
52+
dtor: Option<proc()>,
53+
}
54+
55+
#[unsafe_destructor]
56+
impl Drop for DtorRes {
57+
fn drop(&mut self) {
58+
let dtor = self.dtor.take();
59+
match dtor {
60+
None => (),
61+
Some(f) => f()
62+
}
63+
}
64+
}
65+
66+
impl DtorRes {
67+
fn new(dtor: Option<proc()>) -> DtorRes {
68+
DtorRes {
69+
dtor: dtor,
70+
}
71+
}
72+
}
73+
74+
impl <T> CVec<T> {
75+
/**
76+
* Create a `CVec` from a raw pointer to a buffer with a given length.
77+
*
78+
* Fails if the given pointer is null.
79+
*
80+
* # Arguments
81+
*
82+
* * base - A raw pointer to a buffer
83+
* * len - The number of elements in the buffer
84+
*/
85+
pub unsafe fn new(base: *mut T, len: uint) -> CVec<T> {
86+
assert!(base != ptr::mut_null());
87+
CVec {
88+
base: base,
89+
len: len,
90+
rsrc: DtorRes::new(None)
91+
}
92+
}
93+
94+
/**
95+
* Create a `CVec` from a foreign buffer, with a given length,
96+
* and a function to run upon destruction.
97+
*
98+
* Fails if the given pointer is null.
99+
*
100+
* # Arguments
101+
*
102+
* * base - A foreign pointer to a buffer
103+
* * len - The number of elements in the buffer
104+
* * dtor - A proc to run when the value is destructed, useful
105+
* for freeing the buffer, etc.
106+
*/
107+
pub unsafe fn new_with_dtor(base: *mut T, len: uint, dtor: proc()) -> CVec<T> {
108+
assert!(base != ptr::mut_null());
109+
CVec {
110+
base: base,
111+
len: len,
112+
rsrc: DtorRes::new(Some(dtor))
113+
}
114+
}
115+
116+
/// View the stored data as a slice.
117+
pub fn as_slice<'a>(&'a self) -> &'a [T] {
118+
unsafe {
119+
cast::transmute(raw::Slice { data: self.base as *T, len: self.len })
120+
}
121+
}
122+
123+
/// View the stored data as a mutable slice.
124+
pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [T] {
125+
unsafe {
126+
cast::transmute(raw::Slice { data: self.base as *T, len: self.len })
127+
}
128+
}
129+
130+
/**
131+
* Retrieves an element at a given index
132+
*
133+
* Fails if `ofs` is greater or equal to the length of the vector
134+
*/
135+
pub fn get<'a>(&'a self, ofs: uint) -> &'a T {
136+
assert!(ofs < self.len);
137+
unsafe {
138+
&*self.base.offset(ofs as int)
139+
}
140+
}
141+
142+
/**
143+
* Retrieves a mutable element at a given index
144+
*
145+
* Fails if `ofs` is greater or equal to the length of the vector
146+
*/
147+
pub fn get_mut<'a>(&'a mut self, ofs: uint) -> &'a mut T {
148+
assert!(ofs < self.len);
149+
unsafe {
150+
&mut *self.base.offset(ofs as int)
151+
}
152+
}
153+
154+
/**
155+
* Unwrap the pointer without running the destructor
156+
*
157+
* This method retrieves the underlying pointer, and in the process
158+
* destroys the CVec but without running the destructor. A use case
159+
* would be transferring ownership of the buffer to a C function, as
160+
* in this case you would not want to run the destructor.
161+
*
162+
* Note that if you want to access the underlying pointer without
163+
* cancelling the destructor, you can simply call `transmute` on the return
164+
* value of `get(0)`.
165+
*/
166+
pub unsafe fn unwrap(mut self) -> *mut T {
167+
self.rsrc.dtor = None;
168+
self.base
169+
}
170+
}
171+
172+
impl <T> Container for CVec<T> {
173+
/// Returns the length of the vector
174+
fn len(&self) -> uint { self.len }
175+
}
176+
177+
#[cfg(test)]
178+
mod tests {
179+
use super::*;
180+
181+
use std::libc::*;
182+
use std::libc;
183+
use std::ptr;
184+
use std::rt::global_heap::malloc_raw;
185+
186+
fn malloc(n: uint) -> CVec<u8> {
187+
unsafe {
188+
let mem = malloc_raw(n);
189+
190+
CVec::new_with_dtor(mem as *mut u8, n,
191+
proc() { libc::free(mem as *mut c_void); })
192+
}
193+
}
194+
195+
#[test]
196+
fn test_basic() {
197+
let mut cv = malloc(16);
198+
199+
*cv.get_mut(3) = 8;
200+
*cv.get_mut(4) = 9;
201+
assert_eq!(*cv.get(3), 8);
202+
assert_eq!(*cv.get(4), 9);
203+
assert_eq!(cv.len(), 16);
204+
}
205+
206+
#[test]
207+
#[should_fail]
208+
fn test_fail_at_null() {
209+
unsafe {
210+
CVec::new(ptr::mut_null::<u8>(), 9);
211+
}
212+
}
213+
214+
#[test]
215+
#[should_fail]
216+
fn test_overrun_get() {
217+
let cv = malloc(16);
218+
219+
cv.get(17);
220+
}
221+
222+
#[test]
223+
#[should_fail]
224+
fn test_overrun_set() {
225+
let mut cv = malloc(16);
226+
227+
*cv.get_mut(17) = 0;
228+
}
229+
230+
#[test]
231+
fn test_unwrap() {
232+
unsafe {
233+
let cv = CVec::new_with_dtor(1 as *mut int, 0,
234+
proc() { fail!("Don't run this destructor!") });
235+
let p = cv.unwrap();
236+
assert_eq!(p, 1 as *mut int);
237+
}
238+
}
239+
240+
}

0 commit comments

Comments
 (0)