Skip to content

Commit 1a8e67b

Browse files
committed
---
yaml --- r: 95957 b: refs/heads/dist-snap c: 4f68d13 h: refs/heads/master i: 95955: f573d85 v: v3
1 parent 9f64f6c commit 1a8e67b

File tree

5 files changed

+136
-7
lines changed

5 files changed

+136
-7
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ refs/heads/try: c274a6888410ce3e357e014568b43310ed787d36
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: 147ecfdd8221e4a4d4e090486829a06da1e0ca3c
9-
refs/heads/dist-snap: ca22e947720bd16571f26d71f5de21b135a48fc2
9+
refs/heads/dist-snap: 4f68d1365abc71d0b63c7167c6a4625104912af2
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503
1212
refs/heads/try3: 9387340aab40a73e8424c48fd42f0c521a4875c0

branches/dist-snap/src/libstd/any.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use cast::transmute;
1515
use cmp::Eq;
1616
use option::{Option, Some, None};
17+
use to_bytes::{IterBytes, Cb};
1718
use to_str::ToStr;
1819
use unstable::intrinsics;
1920
use util::Void;
@@ -42,6 +43,12 @@ impl Eq for TypeId {
4243
}
4344
}
4445

46+
impl IterBytes for TypeId {
47+
fn iter_bytes(&self, lsb0: bool, f: Cb) -> bool {
48+
self.t.iter_bytes(lsb0, f)
49+
}
50+
}
51+
4552
///////////////////////////////////////////////////////////////////////////////
4653
// Any trait
4754
///////////////////////////////////////////////////////////////////////////////
@@ -175,6 +182,7 @@ mod tests {
175182
use super::*;
176183
use super::AnyRefExt;
177184
use option::{Some, None};
185+
use hash::Hash;
178186

179187
#[deriving(Eq)]
180188
struct Test;
@@ -197,6 +205,13 @@ mod tests {
197205
assert_eq!(c, f);
198206
}
199207

208+
#[test]
209+
fn type_id_hash() {
210+
let (a, b) = (TypeId::of::<uint>(), TypeId::of::<uint>::());
211+
212+
assert_eq!(a.hash(), b.hash());
213+
}
214+
200215
#[test]
201216
fn any_as_void_ptr() {
202217
let (a, b, c) = (~5u as ~Any, ~TEST as ~Any, ~Test as ~Any);

branches/dist-snap/src/libstd/clone.rs

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,27 @@ pub trait Clone {
2929
/// are copied to maintain uniqueness, while the contents of
3030
/// managed pointers are not copied.
3131
fn clone(&self) -> Self;
32+
33+
/// Perform copy-assignment from `source`.
34+
///
35+
/// `a.clone_from(&b)` is equivalent to `a = b.clone()` in functionality,
36+
/// but can be overriden to reuse the resources of `a` to avoid unnecessary
37+
/// allocations.
38+
#[inline(always)]
39+
fn clone_from(&mut self, source: &Self) {
40+
*self = source.clone()
41+
}
3242
}
3343

3444
impl<T: Clone> Clone for ~T {
35-
/// Return a deep copy of the owned box.
45+
/// Return a copy of the owned box.
3646
#[inline]
3747
fn clone(&self) -> ~T { ~(**self).clone() }
48+
49+
/// Perform copy-assignment from `source` by reusing the existing allocation.
50+
fn clone_from(&mut self, source: &~T) {
51+
**self = (**source).clone()
52+
}
3853
}
3954

4055
impl<T> Clone for @T {
@@ -118,16 +133,31 @@ extern_fn_clone!(A, B, C, D, E, F, G, H)
118133

119134
/// A trait distinct from `Clone` which represents "deep copies" of things like
120135
/// managed boxes which would otherwise not be copied.
121-
pub trait DeepClone {
136+
pub trait DeepClone: Clone {
122137
/// Return a deep copy of the value. Unlike `Clone`, the contents of shared pointer types
123138
/// *are* copied.
124139
fn deep_clone(&self) -> Self;
140+
141+
/// Perform deep copy-assignment from `source`.
142+
///
143+
/// `a.deep_clone_from(&b)` is equivalent to `a = b.deep_clone()` in
144+
/// functionality, but can be overriden to reuse the resources of `a` to
145+
/// avoid unnecessary allocations.
146+
#[inline(always)]
147+
fn deep_clone_from(&mut self, source: &Self) {
148+
*self = source.deep_clone()
149+
}
125150
}
126151

127152
impl<T: DeepClone> DeepClone for ~T {
128153
/// Return a deep copy of the owned box.
129154
#[inline]
130155
fn deep_clone(&self) -> ~T { ~(**self).deep_clone() }
156+
157+
/// Perform deep copy-assignment from `source` by reusing the existing allocation.
158+
fn deep_clone_from(&mut self, source: &~T) {
159+
**self = (**source).deep_clone()
160+
}
131161
}
132162

133163
// FIXME: #6525: should also be implemented for `T: Send + DeepClone`
@@ -234,6 +264,22 @@ fn test_borrowed_clone() {
234264
assert_eq!(*z, 5);
235265
}
236266

267+
#[test]
268+
fn test_clone_from() {
269+
let a = ~5;
270+
let mut b = ~10;
271+
b.clone_from(&a);
272+
assert_eq!(*b, 5);
273+
}
274+
275+
#[test]
276+
fn test_deep_clone_from() {
277+
let a = ~5;
278+
let mut b = ~10;
279+
b.deep_clone_from(&a);
280+
assert_eq!(*b, 5);
281+
}
282+
237283
#[test]
238284
fn test_extern_fn_clone() {
239285
trait Empty {}

branches/dist-snap/src/libstd/os.rs

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -913,41 +913,81 @@ pub fn page_size() -> uint {
913913
}
914914
}
915915

916+
/// A memory mapped file or chunk of memory. This is a very system-specific interface to the OS's
917+
/// memory mapping facilities (`mmap` on POSIX, `VirtualAlloc`/`CreateFileMapping` on win32). It
918+
/// makes no attempt at abstracting platform differences, besides in error values returned. Consider
919+
/// yourself warned.
920+
///
921+
/// The memory map is released (unmapped) when the destructor is run, so don't let it leave scope by
922+
/// accident if you want it to stick around.
916923
pub struct MemoryMap {
924+
/// Pointer to the memory created or modified by this map.
917925
data: *mut u8,
926+
/// Number of bytes this map applies to
918927
len: size_t,
928+
/// Type of mapping
919929
kind: MemoryMapKind
920930
}
921931

932+
/// Type of memory map
922933
pub enum MemoryMapKind {
934+
/// Memory-mapped file. On Windows, the inner pointer is a handle to the mapping, and
935+
/// corresponds to `CreateFileMapping`. Elsewhere, it is null.
923936
MapFile(*c_void),
937+
/// Virtual memory map. Usually used to change the permissions of a given chunk of memory.
938+
/// Corresponds to `VirtualAlloc` on Windows.
924939
MapVirtual
925940
}
926941

942+
/// Options the memory map is created with
927943
pub enum MapOption {
944+
/// The memory should be readable
928945
MapReadable,
946+
/// The memory should be writable
929947
MapWritable,
948+
/// The memory should be executable
930949
MapExecutable,
950+
/// Create a map for a specific address range. Corresponds to `MAP_FIXED` on POSIX.
931951
MapAddr(*c_void),
952+
/// Create a memory mapping for a file with a given fd.
932953
MapFd(c_int),
954+
/// When using `MapFd`, the start of the map is `uint` bytes from the start of the file.
933955
MapOffset(uint)
934956
}
935957

958+
/// Possible errors when creating a map.
936959
pub enum MapError {
937-
// Linux-specific errors
960+
/// ## The following are POSIX-specific
961+
///
962+
/// fd was not open for reading or, if using `MapWritable`, was not open for writing.
938963
ErrFdNotAvail,
964+
/// fd was not valid
939965
ErrInvalidFd,
966+
/// Either the address given by `MapAddr` or offset given by `MapOffset` was not a multiple of
967+
/// `MemoryMap::granularity` (unaligned to page size).
940968
ErrUnaligned,
969+
/// With `MapFd`, the fd does not support mapping.
941970
ErrNoMapSupport,
971+
/// If using `MapAddr`, the address + `min_len` was outside of the process's address space. If
972+
/// using `MapFd`, the target of the fd didn't have enough resources to fulfill the request.
942973
ErrNoMem,
974+
/// Unrecognized error. The inner value is the unrecognized errno.
943975
ErrUnknown(libc::c_int),
944-
945-
// Windows-specific errors
976+
/// ## The following are win32-specific
977+
///
978+
/// Unsupported combination of protection flags (`MapReadable`/`MapWritable`/`MapExecutable`).
946979
ErrUnsupProt,
980+
/// When using `MapFd`, `MapOffset` was given (Windows does not support this at all)
947981
ErrUnsupOffset,
982+
/// When using `MapFd`, there was already a mapping to the file.
948983
ErrAlreadyExists,
984+
/// Unrecognized error from `VirtualAlloc`. The inner value is the return value of GetLastError.
949985
ErrVirtualAlloc(uint),
986+
/// Unrecognized error from `CreateFileMapping`. The inner value is the return value of
987+
/// `GetLastError`.
950988
ErrCreateFileMappingW(uint),
989+
/// Unrecognized error from `MapViewOfFile`. The inner value is the return value of
990+
/// `GetLastError`.
951991
ErrMapViewOfFile(uint)
952992
}
953993

@@ -973,6 +1013,7 @@ impl to_str::ToStr for MapError {
9731013

9741014
#[cfg(unix)]
9751015
impl MemoryMap {
1016+
/// Create a new mapping with the given `options`, at least `min_len` bytes long.
9761017
pub fn new(min_len: uint, options: &[MapOption]) -> Result<MemoryMap, MapError> {
9771018
#[fixed_stack_segment]; #[inline(never)];
9781019

@@ -1028,13 +1069,15 @@ impl MemoryMap {
10281069
}
10291070
}
10301071

1072+
/// Granularity that the offset or address must be for `MapOffset` and `MapAddr` respectively.
10311073
pub fn granularity() -> uint {
10321074
page_size()
10331075
}
10341076
}
10351077

10361078
#[cfg(unix)]
10371079
impl Drop for MemoryMap {
1080+
/// Unmap the mapping. Fails the task if `munmap` fails.
10381081
fn drop(&mut self) {
10391082
#[fixed_stack_segment]; #[inline(never)];
10401083

@@ -1053,6 +1096,7 @@ impl Drop for MemoryMap {
10531096

10541097
#[cfg(windows)]
10551098
impl MemoryMap {
1099+
/// Create a new mapping with the given `options`, at least `min_len` bytes long.
10561100
pub fn new(min_len: uint, options: &[MapOption]) -> Result<MemoryMap, MapError> {
10571101
#[fixed_stack_segment]; #[inline(never)];
10581102

@@ -1161,6 +1205,8 @@ impl MemoryMap {
11611205

11621206
#[cfg(windows)]
11631207
impl Drop for MemoryMap {
1208+
/// Unmap the mapping. Fails the task if any of `VirtualFree`, `UnmapViewOfFile`, or
1209+
/// `CloseHandle` fail.
11641210
fn drop(&mut self) {
11651211
#[fixed_stack_segment]; #[inline(never)];
11661212

branches/dist-snap/src/libstd/vec.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2029,7 +2029,7 @@ impl<'self, T:Clone> MutableCloneableVector<T> for &'self mut [T] {
20292029
#[inline]
20302030
fn copy_from(self, src: &[T]) -> uint {
20312031
for (a, b) in self.mut_iter().zip(src.iter()) {
2032-
*a = b.clone();
2032+
a.clone_from(b);
20332033
}
20342034
cmp::min(self.len(), src.len())
20352035
}
@@ -2282,13 +2282,35 @@ impl<A: Clone> Clone for ~[A] {
22822282
fn clone(&self) -> ~[A] {
22832283
self.iter().map(|item| item.clone()).collect()
22842284
}
2285+
2286+
fn clone_from(&mut self, source: &~[A]) {
2287+
if self.len() < source.len() {
2288+
*self = source.clone()
2289+
} else {
2290+
self.truncate(source.len());
2291+
for (x, y) in self.mut_iter().zip(source.iter()) {
2292+
x.clone_from(y);
2293+
}
2294+
}
2295+
}
22852296
}
22862297

22872298
impl<A: DeepClone> DeepClone for ~[A] {
22882299
#[inline]
22892300
fn deep_clone(&self) -> ~[A] {
22902301
self.iter().map(|item| item.deep_clone()).collect()
22912302
}
2303+
2304+
fn deep_clone_from(&mut self, source: &~[A]) {
2305+
if self.len() < source.len() {
2306+
*self = source.deep_clone()
2307+
} else {
2308+
self.truncate(source.len());
2309+
for (x, y) in self.mut_iter().zip(source.iter()) {
2310+
x.deep_clone_from(y);
2311+
}
2312+
}
2313+
}
22922314
}
22932315

22942316
// This works because every lifetime is a sub-lifetime of 'static

0 commit comments

Comments
 (0)