From 0999ceeb324c8f1b053279b4c63187c9ae5348d6 Mon Sep 17 00:00:00 2001 From: Julian Orth Date: Wed, 11 Mar 2015 03:29:21 +0100 Subject: [PATCH 1/5] add &move RFC --- text/0000-move.md | 402 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 402 insertions(+) create mode 100644 text/0000-move.md diff --git a/text/0000-move.md b/text/0000-move.md new file mode 100644 index 00000000000..470bc6c3180 --- /dev/null +++ b/text/0000-move.md @@ -0,0 +1,402 @@ +- Feature Name: move +- Start Date: Tue Mar 10 23:39:14 CET 2015 +- RFC PR: (leave this empty) +- Rust Issue: (leave this empty) + +# Summary + +Add reference and slice types that take ownership of the objects they reference. + +# Examples + +```rust +fn f() { + let x: String = String::new(); + let x_ref: &move String = &move x; + let y: String = *x_ref; +} + +fn g() { + let xs: [String; 2] = [String::new(), String::new()]; + let x_refs: &move [String] = &move xs[..]; + let ys: Vec = x_refs.into_iter().collect(); +} +``` + +# Motivation + +Today it is not easily possible to express the idea of a reference that you can +move out of. It can be emulated with enums: + +```rust +fn f() { + let x: Option = Some(String::new()); + let x_ref: &mut Option = &mut x; + let y: String = x_ref.take().unwrap(); +} +``` + +There are some disadvantages related to performance and ergonomics: + +- Having to use `Some(variable)` to create a movable reference is inconvenient. +- `&mut Option` is uglier than `&move T`. +- In the code above, the destructors of both `x` and `y` will have to run even + though the `String` has been moved out of `x`. +- `&mut Option` is not safe in the way references are safe in Rust. A `&T` + always points to a valid `T`. A `&mut Option` can point to `None`. The + receiver either has to deal with panics or check (and handle) the presence of + `None`. (Similar to languages such as C# where you can get + `NullReferenceExceptions`.) + +Another problem are traits. The following is currently not possible: + +```rust +trait GivesYouAString { + fn string(self) -> String; +} + +impl GivesYouAString for String { + fn string(self) -> String { self } +} + +fn f() { + let x = String::new(); + let y = &mut x as &mut GivesYouAString; // not object safe + let z = y.string(); // cannot move out of mutable reference +} +``` + +With `&move` this would be easy: + +```rust +trait GivesYouAString { + fn string(&move self) -> String; +} + +impl GivesYouAString for String { + fn string(&move self) -> String { *self } +} + +fn f() { + let x = String::new(); + let y = &move x as &move GivesYouAString; + let z = y.string(); +} +``` + +Right now you can work around this by using: + +```rust +impl GivesYouAString for String { + fn string(self: Box) -> String { *self } +} + +// ... + + let y = Box::new(x) as Box; + let z = y.string(); +} +``` + +but this requires an additional allocation. + +A practical use case of this is the `Any` trait. With move semantics we can pass +an arbitrary type to a function and consume the contained object: + +```rust +fn append_hello_world(x: &move Any) -> String { + if x.is::() { + x.downcast_move::().unwrap().push("hello world") + } else if x.is::() { + x.downcast_move::().unwrap().to_string().push("hello world") + } else { + String::new("hello world") + } +} +``` + +---- + +For slices the situation is somewhat worse. To move out of a slice, the user has +to replace every element in the slice by `Option`. If the size of the slice +is unknown at compile time, then this requires a `Vec>` allocation. +Combining all of these aspects, one might as well just pass a `Vec` directly. + +With `&move` slices and pointers it's quite easy to implement variadic functions +that take arbitrary, type-safe, arguments. + +```rust +/// Collects all of the arguments that are strings into a vector. +fn collect_strings(args: &move [&move Any]) -> Vec { + let mut strings = vec!(); + for arg in args.into_iter() { + if arg.is::() { + let string = x.downcast_move::().unwrap(); + strings.push(string); + } + } + strings +} +``` + +which can be used like this: + +```rust +fn f() { + let arg1 = String::new(); + let arg2 = 1u32; + let arg3 = String::new(); + let strings = collect_strings(&move [&move arg1, &move arg2, &move arg3][..]); +} +``` + +--- + +Even without `&move` slices it is possible to implement variadic functions with +a little bit of compiler magic: + +```rust +fn f(args: ..&move Any) { + // args is an anonymous type that implements Iterator +} + +fn g() { + let arg1 = String::new(); + let arg2 = 1u32; + let arg3 = String::new(); + f(&move arg1, &move arg2, &move arg3); +} +``` + +If `Any` were a built-in type, then this could even be simplified to + +```rust +fn f(args: ..) { + // args is an anonymous type that implements Iterator +} + +fn g() { + let arg1 = String::new(); + let arg2 = 1u32; + let arg3 = String::new(); + f(arg1, arg2, arg3); +} +``` + +But this is somewhat off-topic. + +# Detailed design + +## `&move` references (not a slice) + +A `&move T` reference looks exactly like a `&mut T` reference, i.e., if `T` is a +sized type, then it's simply a pointer to the referenced data. If `T` is a +trait, then it's a fat pointer. + +A `&move T` reference behaves exactly like a `&mut T` reference except that you +can move out of it via dereferencing. This consumes the reference. If the +reference is not explicitly consumed, the compiler will consume the reference +implicitly. For example: + +```rust +fn f(_x: &move String) { +} +``` + +Modulo optimization, this is equivalent to + +```rust +fn f(_x: &move String) { + let _unused = *_x; +} +``` + +Creating a `&move T` reference makes the referenced object indefinitely +inaccessible except through the `&move` reference. + +```rust +fn f(x: String) { + { + let _y = &move x; + } + x.push(""); // error: x has been moved +} +``` + +Creating a `&move T` reference moves the drop obligation for the referenced +object into the reference. For example: + +```rust +fn f(x: String, flag: bool) { + if flag { + g(&move x); + } else { + h(); + } +} +``` + +Assuming non-zeroing drop, the compiler will generate the following pseudo code: + +```rust +fn f(x: String, flag: bool) { + let mut x_needs_drop = true; + + if flag { + x_needs_drop = false; + g(&move x); + } else { + h(); + } + + if x_needs_drop { + drop x; + // This is pseudo code because the user cannot access `x` at this point. + } +} +``` + +Creating a `&move T` reference, however, does not move the drop obligation for +the container into the reference. For example: + +```rust +fn f(x: Box, flag: bool) { + if flag { + g(&move *x); + } else { + h(); + } +} +``` + +Again assuming non-zeroing drops, the compiler generates + +```rust +fn f(x: Box, flag: bool) { + let mut x_needs_drop = true; + + if flag { + x_needs_drop = false; + g(&move *x); + } else { + h(); + } + + if x_needs_drop { + drop *x; + } + deallocate(x); +} +``` + +This means that there are two drop flags: One for the container and one for the +contained object. (With zeroing drops both of these flags are implicit.) + +It's possible to take a sub-`&move` reference if the type does not implement +`Drop`. For example: + +```rust +struct X { + a: String, + b: String, +} + +fn f(x: &move X) -> &move String { + &move x.b +} + +// This is equivalent to + +fn g(x: &move X) -> &move String { + let X { a: _, b: ref move b } = *x; + b +} + +// One could even write + +fn h(x: &move X) -> (&move String, &move String) { + let a = &move x.a; + let b = &move x.b; + (a, b) +} +``` + +A `&move T` supports implicit coercions to `&mut T` and `&T`: + +```rust +fn f(x: String) { + g(&move x); + // implicit move out of the anonymous reference here +} + +fn g(x: &mut T) { +} +``` + +Similarly, + +```rust +trait DerefMove { + type Target; + + fn deref_move(&move self) -> &move Target; +} +``` + +supports deref coercion between `&move` references. + +### Custom containers + +Rust currently does not support custom containers, e.g., you cannot do things +like + +```rust +fn f(x: Container) { + let y: String = *x; +} +``` + +This is because the `Drop` implementation of `Container` cannot +distinguish between cases where it has to drop the contained string and where +the string has been moved out. + +Note that `Box` is a special compiler built-in type for this reason. + +This limits the usefulness of `DerefMove` because you cannot `deref_move` if the +container has a custom `Drop` implementation. + +## `&move` slices + +A `&move [T]` slice looks exactly like a `&mut [T]` slice. That is, it's a fat +pointer with a data pointer and a length. + +A `&move [T]` additionally comes with an `into_iter` method that returns an +object which implements `Iterator`. + +A `&move` slice has similar semantics as an ordinary `&move T` reference, but +its drop obligation includes all elements in the slice. Taking a sub-slice +implies that all elements that are no longer in the slice will be dropped at +this point. + +At this point, it's not possible to create `&move` slices from anything but +`[T; N]` and `Box<[T]>`. This is because of the custom container restriction +mentioned in the section above. + +# Drawbacks + +`&move` slices are very limited at the moment. Once Rust supports custom +containers they become more powerful. + +# Alternatives + +## What other designs have been considered? + +None + +## What is the impact of not doing this? + +Can't move out of references. + +# Unresolved questions + +None right now. From da1d66ac290572fad43b64222340ac296adf44dd Mon Sep 17 00:00:00 2001 From: Julian Orth Date: Wed, 11 Mar 2015 15:38:37 +0100 Subject: [PATCH 2/5] fixup --- text/0000-move.md | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/text/0000-move.md b/text/0000-move.md index 470bc6c3180..84d692c410a 100644 --- a/text/0000-move.md +++ b/text/0000-move.md @@ -187,11 +187,9 @@ But this is somewhat off-topic. # Detailed design -## `&move` references (not a slice) - A `&move T` reference looks exactly like a `&mut T` reference, i.e., if `T` is a sized type, then it's simply a pointer to the referenced data. If `T` is a -trait, then it's a fat pointer. +trait or slice (`[U]`), then it's a fat pointer. A `&move T` reference behaves exactly like a `&mut T` reference except that you can move out of it via dereferencing. This consumes the reference. If the @@ -345,7 +343,7 @@ trait DerefMove { supports deref coercion between `&move` references. -### Custom containers +## Custom containers Rust currently does not support custom containers, e.g., you cannot do things like @@ -365,18 +363,21 @@ Note that `Box` is a special compiler built-in type for this reason. This limits the usefulness of `DerefMove` because you cannot `deref_move` if the container has a custom `Drop` implementation. -## `&move` slices +## Slices + +Even though slices are not special, we'll describe the behavior that can be +expected from `&move [T]` slices here: -A `&move [T]` slice looks exactly like a `&mut [T]` slice. That is, it's a fat -pointer with a data pointer and a length. +Subslicing causes the objects that are no longer in the slice to be dropped. -A `&move [T]` additionally comes with an `into_iter` method that returns an -object which implements `Iterator`. +Taking a move reference to a single element causes all other elements to be +dropped: -A `&move` slice has similar semantics as an ordinary `&move T` reference, but -its drop obligation includes all elements in the slice. Taking a sub-slice -implies that all elements that are no longer in the slice will be dropped at -this point. +```rust +fn f(xs: &move [String]) { + let x = &move xs[0]; +} +``` At this point, it's not possible to create `&move` slices from anything but `[T; N]` and `Box<[T]>`. This is because of the custom container restriction From 9ed5f8bc50cb6a89ba864ba14a57ae69ee6fd855 Mon Sep 17 00:00:00 2001 From: Julian Orth Date: Wed, 11 Mar 2015 17:10:50 +0100 Subject: [PATCH 3/5] rename &move to &out --- text/{0000-move.md => 0000-out.md} | 98 +++++++++++++++--------------- 1 file changed, 49 insertions(+), 49 deletions(-) rename text/{0000-move.md => 0000-out.md} (76%) diff --git a/text/0000-move.md b/text/0000-out.md similarity index 76% rename from text/0000-move.md rename to text/0000-out.md index 84d692c410a..8976e29c707 100644 --- a/text/0000-move.md +++ b/text/0000-out.md @@ -1,4 +1,4 @@ -- Feature Name: move +- Feature Name: out - Start Date: Tue Mar 10 23:39:14 CET 2015 - RFC PR: (leave this empty) - Rust Issue: (leave this empty) @@ -12,13 +12,13 @@ Add reference and slice types that take ownership of the objects they reference. ```rust fn f() { let x: String = String::new(); - let x_ref: &move String = &move x; + let x_ref: &out String = &out x; let y: String = *x_ref; } fn g() { let xs: [String; 2] = [String::new(), String::new()]; - let x_refs: &move [String] = &move xs[..]; + let x_refs: &out [String] = &out xs[..]; let ys: Vec = x_refs.into_iter().collect(); } ``` @@ -39,7 +39,7 @@ fn f() { There are some disadvantages related to performance and ergonomics: - Having to use `Some(variable)` to create a movable reference is inconvenient. -- `&mut Option` is uglier than `&move T`. +- `&mut Option` is uglier than `&out T`. - In the code above, the destructors of both `x` and `y` will have to run even though the `String` has been moved out of `x`. - `&mut Option` is not safe in the way references are safe in Rust. A `&T` @@ -66,20 +66,20 @@ fn f() { } ``` -With `&move` this would be easy: +With `&out` this would be easy: ```rust trait GivesYouAString { - fn string(&move self) -> String; + fn string(&out self) -> String; } impl GivesYouAString for String { - fn string(&move self) -> String { *self } + fn string(&out self) -> String { *self } } fn f() { let x = String::new(); - let y = &move x as &move GivesYouAString; + let y = &out x as &out GivesYouAString; let z = y.string(); } ``` @@ -104,7 +104,7 @@ A practical use case of this is the `Any` trait. With move semantics we can pass an arbitrary type to a function and consume the contained object: ```rust -fn append_hello_world(x: &move Any) -> String { +fn append_hello_world(x: &out Any) -> String { if x.is::() { x.downcast_move::().unwrap().push("hello world") } else if x.is::() { @@ -122,12 +122,12 @@ to replace every element in the slice by `Option`. If the size of the slice is unknown at compile time, then this requires a `Vec>` allocation. Combining all of these aspects, one might as well just pass a `Vec` directly. -With `&move` slices and pointers it's quite easy to implement variadic functions +With `&out` slices and pointers it's quite easy to implement variadic functions that take arbitrary, type-safe, arguments. ```rust /// Collects all of the arguments that are strings into a vector. -fn collect_strings(args: &move [&move Any]) -> Vec { +fn collect_strings(args: &out [&out Any]) -> Vec { let mut strings = vec!(); for arg in args.into_iter() { if arg.is::() { @@ -146,25 +146,25 @@ fn f() { let arg1 = String::new(); let arg2 = 1u32; let arg3 = String::new(); - let strings = collect_strings(&move [&move arg1, &move arg2, &move arg3][..]); + let strings = collect_strings(&out [&out arg1, &out arg2, &out arg3][..]); } ``` --- -Even without `&move` slices it is possible to implement variadic functions with +Even without `&out` slices it is possible to implement variadic functions with a little bit of compiler magic: ```rust -fn f(args: ..&move Any) { - // args is an anonymous type that implements Iterator +fn f(args: ..&out Any) { + // args is an anonymous type that implements Iterator } fn g() { let arg1 = String::new(); let arg2 = 1u32; let arg3 = String::new(); - f(&move arg1, &move arg2, &move arg3); + f(&out arg1, &out arg2, &out arg3); } ``` @@ -172,7 +172,7 @@ If `Any` were a built-in type, then this could even be simplified to ```rust fn f(args: ..) { - // args is an anonymous type that implements Iterator + // args is an anonymous type that implements Iterator } fn g() { @@ -187,47 +187,47 @@ But this is somewhat off-topic. # Detailed design -A `&move T` reference looks exactly like a `&mut T` reference, i.e., if `T` is a +A `&out T` reference looks exactly like a `&mut T` reference, i.e., if `T` is a sized type, then it's simply a pointer to the referenced data. If `T` is a trait or slice (`[U]`), then it's a fat pointer. -A `&move T` reference behaves exactly like a `&mut T` reference except that you +A `&out T` reference behaves exactly like a `&mut T` reference except that you can move out of it via dereferencing. This consumes the reference. If the reference is not explicitly consumed, the compiler will consume the reference implicitly. For example: ```rust -fn f(_x: &move String) { +fn f(_x: &out String) { } ``` Modulo optimization, this is equivalent to ```rust -fn f(_x: &move String) { +fn f(_x: &out String) { let _unused = *_x; } ``` -Creating a `&move T` reference makes the referenced object indefinitely -inaccessible except through the `&move` reference. +Creating a `&out T` reference makes the referenced object indefinitely +inaccessible except through the `&out` reference. ```rust fn f(x: String) { { - let _y = &move x; + let _y = &out x; } x.push(""); // error: x has been moved } ``` -Creating a `&move T` reference moves the drop obligation for the referenced +Creating a `&out T` reference moves the drop obligation for the referenced object into the reference. For example: ```rust fn f(x: String, flag: bool) { if flag { - g(&move x); + g(&out x); } else { h(); } @@ -242,7 +242,7 @@ fn f(x: String, flag: bool) { if flag { x_needs_drop = false; - g(&move x); + g(&out x); } else { h(); } @@ -254,13 +254,13 @@ fn f(x: String, flag: bool) { } ``` -Creating a `&move T` reference, however, does not move the drop obligation for +Creating a `&out T` reference, however, does not move the drop obligation for the container into the reference. For example: ```rust fn f(x: Box, flag: bool) { if flag { - g(&move *x); + g(&out *x); } else { h(); } @@ -275,7 +275,7 @@ fn f(x: Box, flag: bool) { if flag { x_needs_drop = false; - g(&move *x); + g(&out *x); } else { h(); } @@ -290,7 +290,7 @@ fn f(x: Box, flag: bool) { This means that there are two drop flags: One for the container and one for the contained object. (With zeroing drops both of these flags are implicit.) -It's possible to take a sub-`&move` reference if the type does not implement +It's possible to take a sub-`&out` reference if the type does not implement `Drop`. For example: ```rust @@ -299,31 +299,31 @@ struct X { b: String, } -fn f(x: &move X) -> &move String { - &move x.b +fn f(x: &out X) -> &out String { + &out x.b } // This is equivalent to -fn g(x: &move X) -> &move String { - let X { a: _, b: ref move b } = *x; +fn g(x: &out X) -> &out String { + let X { a: _, b: ref out b } = *x; b } // One could even write -fn h(x: &move X) -> (&move String, &move String) { - let a = &move x.a; - let b = &move x.b; +fn h(x: &out X) -> (&out String, &out String) { + let a = &out x.a; + let b = &out x.b; (a, b) } ``` -A `&move T` supports implicit coercions to `&mut T` and `&T`: +A `&out T` supports implicit coercions to `&mut T` and `&T`: ```rust fn f(x: String) { - g(&move x); + g(&out x); // implicit move out of the anonymous reference here } @@ -334,14 +334,14 @@ fn g(x: &mut T) { Similarly, ```rust -trait DerefMove { +trait DerefOut { type Target; - fn deref_move(&move self) -> &move Target; + fn deref_out(&out self) -> &out Target; } ``` -supports deref coercion between `&move` references. +supports deref coercion between `&out` references. ## Custom containers @@ -360,13 +360,13 @@ the string has been moved out. Note that `Box` is a special compiler built-in type for this reason. -This limits the usefulness of `DerefMove` because you cannot `deref_move` if the +This limits the usefulness of `DerefMove` because you cannot `deref_out` if the container has a custom `Drop` implementation. ## Slices Even though slices are not special, we'll describe the behavior that can be -expected from `&move [T]` slices here: +expected from `&out [T]` slices here: Subslicing causes the objects that are no longer in the slice to be dropped. @@ -374,18 +374,18 @@ Taking a move reference to a single element causes all other elements to be dropped: ```rust -fn f(xs: &move [String]) { - let x = &move xs[0]; +fn f(xs: &out [String]) { + let x = &out xs[0]; } ``` -At this point, it's not possible to create `&move` slices from anything but +At this point, it's not possible to create `&out` slices from anything but `[T; N]` and `Box<[T]>`. This is because of the custom container restriction mentioned in the section above. # Drawbacks -`&move` slices are very limited at the moment. Once Rust supports custom +`&out` slices are very limited at the moment. Once Rust supports custom containers they become more powerful. # Alternatives From e1d4f1a5690201b8f6cefac26e257107f5d81ca0 Mon Sep 17 00:00:00 2001 From: Julian Orth Date: Wed, 11 Mar 2015 23:34:40 +0100 Subject: [PATCH 4/5] rename again. this time to &own ...... --- text/0000-out.md | 94 ++++++++++++++++++++++++------------------------ 1 file changed, 47 insertions(+), 47 deletions(-) diff --git a/text/0000-out.md b/text/0000-out.md index 8976e29c707..050a048df86 100644 --- a/text/0000-out.md +++ b/text/0000-out.md @@ -1,4 +1,4 @@ -- Feature Name: out +- Feature Name: own - Start Date: Tue Mar 10 23:39:14 CET 2015 - RFC PR: (leave this empty) - Rust Issue: (leave this empty) @@ -12,13 +12,13 @@ Add reference and slice types that take ownership of the objects they reference. ```rust fn f() { let x: String = String::new(); - let x_ref: &out String = &out x; + let x_ref: &own String = &own x; let y: String = *x_ref; } fn g() { let xs: [String; 2] = [String::new(), String::new()]; - let x_refs: &out [String] = &out xs[..]; + let x_refs: &own [String] = &own xs[..]; let ys: Vec = x_refs.into_iter().collect(); } ``` @@ -39,7 +39,7 @@ fn f() { There are some disadvantages related to performance and ergonomics: - Having to use `Some(variable)` to create a movable reference is inconvenient. -- `&mut Option` is uglier than `&out T`. +- `&mut Option` is uglier than `&own T`. - In the code above, the destructors of both `x` and `y` will have to run even though the `String` has been moved out of `x`. - `&mut Option` is not safe in the way references are safe in Rust. A `&T` @@ -66,20 +66,20 @@ fn f() { } ``` -With `&out` this would be easy: +With `&own` this would be easy: ```rust trait GivesYouAString { - fn string(&out self) -> String; + fn string(&own self) -> String; } impl GivesYouAString for String { - fn string(&out self) -> String { *self } + fn string(&own self) -> String { *self } } fn f() { let x = String::new(); - let y = &out x as &out GivesYouAString; + let y = &own x as &own GivesYouAString; let z = y.string(); } ``` @@ -104,7 +104,7 @@ A practical use case of this is the `Any` trait. With move semantics we can pass an arbitrary type to a function and consume the contained object: ```rust -fn append_hello_world(x: &out Any) -> String { +fn append_hello_world(x: &own Any) -> String { if x.is::() { x.downcast_move::().unwrap().push("hello world") } else if x.is::() { @@ -122,12 +122,12 @@ to replace every element in the slice by `Option`. If the size of the slice is unknown at compile time, then this requires a `Vec>` allocation. Combining all of these aspects, one might as well just pass a `Vec` directly. -With `&out` slices and pointers it's quite easy to implement variadic functions +With `&own` slices and pointers it's quite easy to implement variadic functions that take arbitrary, type-safe, arguments. ```rust /// Collects all of the arguments that are strings into a vector. -fn collect_strings(args: &out [&out Any]) -> Vec { +fn collect_strings(args: &own [&own Any]) -> Vec { let mut strings = vec!(); for arg in args.into_iter() { if arg.is::() { @@ -146,25 +146,25 @@ fn f() { let arg1 = String::new(); let arg2 = 1u32; let arg3 = String::new(); - let strings = collect_strings(&out [&out arg1, &out arg2, &out arg3][..]); + let strings = collect_strings(&own [&own arg1, &own arg2, &own arg3][..]); } ``` --- -Even without `&out` slices it is possible to implement variadic functions with +Even without `&own` slices it is possible to implement variadic functions with a little bit of compiler magic: ```rust -fn f(args: ..&out Any) { - // args is an anonymous type that implements Iterator +fn f(args: ..&own Any) { + // args is an anonymous type that implements Iterator } fn g() { let arg1 = String::new(); let arg2 = 1u32; let arg3 = String::new(); - f(&out arg1, &out arg2, &out arg3); + f(&own arg1, &own arg2, &own arg3); } ``` @@ -172,7 +172,7 @@ If `Any` were a built-in type, then this could even be simplified to ```rust fn f(args: ..) { - // args is an anonymous type that implements Iterator + // args is an anonymous type that implements Iterator } fn g() { @@ -187,47 +187,47 @@ But this is somewhat off-topic. # Detailed design -A `&out T` reference looks exactly like a `&mut T` reference, i.e., if `T` is a +A `&own T` reference looks exactly like a `&mut T` reference, i.e., if `T` is a sized type, then it's simply a pointer to the referenced data. If `T` is a trait or slice (`[U]`), then it's a fat pointer. -A `&out T` reference behaves exactly like a `&mut T` reference except that you +A `&own T` reference behaves exactly like a `&mut T` reference except that you can move out of it via dereferencing. This consumes the reference. If the reference is not explicitly consumed, the compiler will consume the reference implicitly. For example: ```rust -fn f(_x: &out String) { +fn f(_x: &own String) { } ``` Modulo optimization, this is equivalent to ```rust -fn f(_x: &out String) { +fn f(_x: &own String) { let _unused = *_x; } ``` -Creating a `&out T` reference makes the referenced object indefinitely -inaccessible except through the `&out` reference. +Creating a `&own T` reference makes the referenced object indefinitely +inaccessible except through the `&own` reference. ```rust fn f(x: String) { { - let _y = &out x; + let _y = &own x; } x.push(""); // error: x has been moved } ``` -Creating a `&out T` reference moves the drop obligation for the referenced +Creating a `&own T` reference moves the drop obligation for the referenced object into the reference. For example: ```rust fn f(x: String, flag: bool) { if flag { - g(&out x); + g(&own x); } else { h(); } @@ -242,7 +242,7 @@ fn f(x: String, flag: bool) { if flag { x_needs_drop = false; - g(&out x); + g(&own x); } else { h(); } @@ -254,13 +254,13 @@ fn f(x: String, flag: bool) { } ``` -Creating a `&out T` reference, however, does not move the drop obligation for +Creating a `&own T` reference, however, does not move the drop obligation for the container into the reference. For example: ```rust fn f(x: Box, flag: bool) { if flag { - g(&out *x); + g(&own *x); } else { h(); } @@ -275,7 +275,7 @@ fn f(x: Box, flag: bool) { if flag { x_needs_drop = false; - g(&out *x); + g(&own *x); } else { h(); } @@ -290,7 +290,7 @@ fn f(x: Box, flag: bool) { This means that there are two drop flags: One for the container and one for the contained object. (With zeroing drops both of these flags are implicit.) -It's possible to take a sub-`&out` reference if the type does not implement +It's possible to take a sub-`&own` reference if the type does not implement `Drop`. For example: ```rust @@ -299,31 +299,31 @@ struct X { b: String, } -fn f(x: &out X) -> &out String { - &out x.b +fn f(x: &own X) -> &own String { + &own x.b } // This is equivalent to -fn g(x: &out X) -> &out String { +fn g(x: &own X) -> &own String { let X { a: _, b: ref out b } = *x; b } // One could even write -fn h(x: &out X) -> (&out String, &out String) { - let a = &out x.a; - let b = &out x.b; +fn h(x: &own X) -> (&own String, &own String) { + let a = &own x.a; + let b = &own x.b; (a, b) } ``` -A `&out T` supports implicit coercions to `&mut T` and `&T`: +A `&own T` supports implicit coercions to `&mut T` and `&T`: ```rust fn f(x: String) { - g(&out x); + g(&own x); // implicit move out of the anonymous reference here } @@ -337,11 +337,11 @@ Similarly, trait DerefOut { type Target; - fn deref_out(&out self) -> &out Target; + fn deref_out(&own self) -> &own Target; } ``` -supports deref coercion between `&out` references. +supports deref coercion between `&own` references. ## Custom containers @@ -366,26 +366,26 @@ container has a custom `Drop` implementation. ## Slices Even though slices are not special, we'll describe the behavior that can be -expected from `&out [T]` slices here: +expected from `&own [T]` slices here: Subslicing causes the objects that are no longer in the slice to be dropped. -Taking a move reference to a single element causes all other elements to be +Taking a `&own` reference to a single element causes all other elements to be dropped: ```rust -fn f(xs: &out [String]) { - let x = &out xs[0]; +fn f(xs: &own [String]) { + let x = &own xs[0]; } ``` -At this point, it's not possible to create `&out` slices from anything but +At this point, it's not possible to create `&own` slices from anything but `[T; N]` and `Box<[T]>`. This is because of the custom container restriction mentioned in the section above. # Drawbacks -`&out` slices are very limited at the moment. Once Rust supports custom +`&own` slices are very limited at the moment. Once Rust supports custom containers they become more powerful. # Alternatives From 9068e8737a043dac783228efe938d6d2b8ce6488 Mon Sep 17 00:00:00 2001 From: Julian Orth Date: Thu, 12 Mar 2015 01:15:43 +0100 Subject: [PATCH 5/5] slices are more powerful than previously thought --- text/{0000-out.md => 0000-own.md} | 56 +++++++++++-------------------- 1 file changed, 19 insertions(+), 37 deletions(-) rename text/{0000-out.md => 0000-own.md} (88%) diff --git a/text/0000-out.md b/text/0000-own.md similarity index 88% rename from text/0000-out.md rename to text/0000-own.md index 050a048df86..02f7250a100 100644 --- a/text/0000-out.md +++ b/text/0000-own.md @@ -331,38 +331,6 @@ fn g(x: &mut T) { } ``` -Similarly, - -```rust -trait DerefOut { - type Target; - - fn deref_out(&own self) -> &own Target; -} -``` - -supports deref coercion between `&own` references. - -## Custom containers - -Rust currently does not support custom containers, e.g., you cannot do things -like - -```rust -fn f(x: Container) { - let y: String = *x; -} -``` - -This is because the `Drop` implementation of `Container` cannot -distinguish between cases where it has to drop the contained string and where -the string has been moved out. - -Note that `Box` is a special compiler built-in type for this reason. - -This limits the usefulness of `DerefMove` because you cannot `deref_out` if the -container has a custom `Drop` implementation. - ## Slices Even though slices are not special, we'll describe the behavior that can be @@ -379,14 +347,28 @@ fn f(xs: &own [String]) { } ``` -At this point, it's not possible to create `&own` slices from anything but -`[T; N]` and `Box<[T]>`. This is because of the custom container restriction -mentioned in the section above. +At the moment it is not possible to move out of custom containers such as +`VecDeque`. However, this is not a serious problem because we can still +create owning slices via a method: + +```rust +impl VecDeque { + pub fn owned_sices(&mut self) -> (&own [T], &own [T]) { + let slices = mem::transmute(self.as_slices()); + self.tail = 0; + self.head = 0; + slices + } +} +``` + +The two slices that have been returned own their content and once the slices +have been dropped the empty `VecDeque` is accessible again. # Drawbacks -`&own` slices are very limited at the moment. Once Rust supports custom -containers they become more powerful. +You can't create `&own` slices with the `&own vec[..]` syntax because Rust +currently does not allow moving out of containers other that `Box`. # Alternatives