Skip to content

Commit f774cdd

Browse files
author
Clar Charr
committed
Move placement new operators to module.
1 parent d460aca commit f774cdd

File tree

2 files changed

+130
-119
lines changed

2 files changed

+130
-119
lines changed

src/libcore/ops/mod.rs

Lines changed: 4 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -150,17 +150,16 @@
150150
mod arith;
151151
mod bit;
152152
mod function;
153+
mod place;
153154
mod range;
154155

155156
#[stable(feature = "rust1", since = "1.0.0")]
156157
pub use self::arith::{Add, Sub, Mul, Div, Rem, Neg};
157-
158158
#[stable(feature = "op_assign_traits", since = "1.8.0")]
159159
pub use self::arith::{AddAssign, SubAssign, MulAssign, DivAssign, RemAssign};
160160

161161
#[stable(feature = "rust1", since = "1.0.0")]
162162
pub use self::bit::{Not, BitAnd, BitOr, BitXor, Shl, Shr};
163-
164163
#[stable(feature = "op_assign_traits", since = "1.8.0")]
165164
pub use self::bit::{BitAndAssign, BitOrAssign, BitXorAssign, ShlAssign, ShrAssign};
166165

@@ -173,6 +172,9 @@ pub use self::range::{Range, RangeFrom, RangeFull, RangeTo};
173172
#[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")]
174173
pub use self::range::{RangeInclusive, RangeToInclusive};
175174

175+
#[unstable(feature = "placement_new_protocol", issue = "27779")]
176+
pub use self::place::{Place, Placer, InPlace, Boxed, BoxPlace};
177+
176178
use marker::Unsize;
177179

178180
/// The `Drop` trait is used to run some code when a value goes out of scope.
@@ -592,123 +594,6 @@ impl<T: ?Sized+Unsize<U>, U: ?Sized> CoerceUnsized<*const U> for *mut T {}
592594
#[unstable(feature = "coerce_unsized", issue = "27732")]
593595
impl<T: ?Sized+Unsize<U>, U: ?Sized> CoerceUnsized<*const U> for *const T {}
594596

595-
/// Both `PLACE <- EXPR` and `box EXPR` desugar into expressions
596-
/// that allocate an intermediate "place" that holds uninitialized
597-
/// state. The desugaring evaluates EXPR, and writes the result at
598-
/// the address returned by the `pointer` method of this trait.
599-
///
600-
/// A `Place` can be thought of as a special representation for a
601-
/// hypothetical `&uninit` reference (which Rust cannot currently
602-
/// express directly). That is, it represents a pointer to
603-
/// uninitialized storage.
604-
///
605-
/// The client is responsible for two steps: First, initializing the
606-
/// payload (it can access its address via `pointer`). Second,
607-
/// converting the agent to an instance of the owning pointer, via the
608-
/// appropriate `finalize` method (see the `InPlace`.
609-
///
610-
/// If evaluating EXPR fails, then it is up to the destructor for the
611-
/// implementation of Place to clean up any intermediate state
612-
/// (e.g. deallocate box storage, pop a stack, etc).
613-
#[unstable(feature = "placement_new_protocol", issue = "27779")]
614-
pub trait Place<Data: ?Sized> {
615-
/// Returns the address where the input value will be written.
616-
/// Note that the data at this address is generally uninitialized,
617-
/// and thus one should use `ptr::write` for initializing it.
618-
fn pointer(&mut self) -> *mut Data;
619-
}
620-
621-
/// Interface to implementations of `PLACE <- EXPR`.
622-
///
623-
/// `PLACE <- EXPR` effectively desugars into:
624-
///
625-
/// ```rust,ignore
626-
/// let p = PLACE;
627-
/// let mut place = Placer::make_place(p);
628-
/// let raw_place = Place::pointer(&mut place);
629-
/// let value = EXPR;
630-
/// unsafe {
631-
/// std::ptr::write(raw_place, value);
632-
/// InPlace::finalize(place)
633-
/// }
634-
/// ```
635-
///
636-
/// The type of `PLACE <- EXPR` is derived from the type of `PLACE`;
637-
/// if the type of `PLACE` is `P`, then the final type of the whole
638-
/// expression is `P::Place::Owner` (see the `InPlace` and `Boxed`
639-
/// traits).
640-
///
641-
/// Values for types implementing this trait usually are transient
642-
/// intermediate values (e.g. the return value of `Vec::emplace_back`)
643-
/// or `Copy`, since the `make_place` method takes `self` by value.
644-
#[unstable(feature = "placement_new_protocol", issue = "27779")]
645-
pub trait Placer<Data: ?Sized> {
646-
/// `Place` is the intermedate agent guarding the
647-
/// uninitialized state for `Data`.
648-
type Place: InPlace<Data>;
649-
650-
/// Creates a fresh place from `self`.
651-
fn make_place(self) -> Self::Place;
652-
}
653-
654-
/// Specialization of `Place` trait supporting `PLACE <- EXPR`.
655-
#[unstable(feature = "placement_new_protocol", issue = "27779")]
656-
pub trait InPlace<Data: ?Sized>: Place<Data> {
657-
/// `Owner` is the type of the end value of `PLACE <- EXPR`
658-
///
659-
/// Note that when `PLACE <- EXPR` is solely used for
660-
/// side-effecting an existing data-structure,
661-
/// e.g. `Vec::emplace_back`, then `Owner` need not carry any
662-
/// information at all (e.g. it can be the unit type `()` in that
663-
/// case).
664-
type Owner;
665-
666-
/// Converts self into the final value, shifting
667-
/// deallocation/cleanup responsibilities (if any remain), over to
668-
/// the returned instance of `Owner` and forgetting self.
669-
unsafe fn finalize(self) -> Self::Owner;
670-
}
671-
672-
/// Core trait for the `box EXPR` form.
673-
///
674-
/// `box EXPR` effectively desugars into:
675-
///
676-
/// ```rust,ignore
677-
/// let mut place = BoxPlace::make_place();
678-
/// let raw_place = Place::pointer(&mut place);
679-
/// let value = EXPR;
680-
/// unsafe {
681-
/// ::std::ptr::write(raw_place, value);
682-
/// Boxed::finalize(place)
683-
/// }
684-
/// ```
685-
///
686-
/// The type of `box EXPR` is supplied from its surrounding
687-
/// context; in the above expansion, the result type `T` is used
688-
/// to determine which implementation of `Boxed` to use, and that
689-
/// `<T as Boxed>` in turn dictates determines which
690-
/// implementation of `BoxPlace` to use, namely:
691-
/// `<<T as Boxed>::Place as BoxPlace>`.
692-
#[unstable(feature = "placement_new_protocol", issue = "27779")]
693-
pub trait Boxed {
694-
/// The kind of data that is stored in this kind of box.
695-
type Data; /* (`Data` unused b/c cannot yet express below bound.) */
696-
/// The place that will negotiate the storage of the data.
697-
type Place: BoxPlace<Self::Data>;
698-
699-
/// Converts filled place into final owning value, shifting
700-
/// deallocation/cleanup responsibilities (if any remain), over to
701-
/// returned instance of `Self` and forgetting `filled`.
702-
unsafe fn finalize(filled: Self::Place) -> Self;
703-
}
704-
705-
/// Specialization of `Place` trait supporting `box EXPR`.
706-
#[unstable(feature = "placement_new_protocol", issue = "27779")]
707-
pub trait BoxPlace<Data: ?Sized> : Place<Data> {
708-
/// Creates a globally fresh place.
709-
fn make_place() -> Self;
710-
}
711-
712597
/// This trait has been superseded by the `Try` trait, but must remain
713598
/// here as `?` is still lowered to it in stage0 .
714599
#[cfg(stage0)]

src/libcore/ops/place.rs

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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+
/// Both `PLACE <- EXPR` and `box EXPR` desugar into expressions
12+
/// that allocate an intermediate "place" that holds uninitialized
13+
/// state. The desugaring evaluates EXPR, and writes the result at
14+
/// the address returned by the `pointer` method of this trait.
15+
///
16+
/// A `Place` can be thought of as a special representation for a
17+
/// hypothetical `&uninit` reference (which Rust cannot currently
18+
/// express directly). That is, it represents a pointer to
19+
/// uninitialized storage.
20+
///
21+
/// The client is responsible for two steps: First, initializing the
22+
/// payload (it can access its address via `pointer`). Second,
23+
/// converting the agent to an instance of the owning pointer, via the
24+
/// appropriate `finalize` method (see the `InPlace`.
25+
///
26+
/// If evaluating EXPR fails, then it is up to the destructor for the
27+
/// implementation of Place to clean up any intermediate state
28+
/// (e.g. deallocate box storage, pop a stack, etc).
29+
#[unstable(feature = "placement_new_protocol", issue = "27779")]
30+
pub trait Place<Data: ?Sized> {
31+
/// Returns the address where the input value will be written.
32+
/// Note that the data at this address is generally uninitialized,
33+
/// and thus one should use `ptr::write` for initializing it.
34+
fn pointer(&mut self) -> *mut Data;
35+
}
36+
37+
/// Interface to implementations of `PLACE <- EXPR`.
38+
///
39+
/// `PLACE <- EXPR` effectively desugars into:
40+
///
41+
/// ```rust,ignore
42+
/// let p = PLACE;
43+
/// let mut place = Placer::make_place(p);
44+
/// let raw_place = Place::pointer(&mut place);
45+
/// let value = EXPR;
46+
/// unsafe {
47+
/// std::ptr::write(raw_place, value);
48+
/// InPlace::finalize(place)
49+
/// }
50+
/// ```
51+
///
52+
/// The type of `PLACE <- EXPR` is derived from the type of `PLACE`;
53+
/// if the type of `PLACE` is `P`, then the final type of the whole
54+
/// expression is `P::Place::Owner` (see the `InPlace` and `Boxed`
55+
/// traits).
56+
///
57+
/// Values for types implementing this trait usually are transient
58+
/// intermediate values (e.g. the return value of `Vec::emplace_back`)
59+
/// or `Copy`, since the `make_place` method takes `self` by value.
60+
#[unstable(feature = "placement_new_protocol", issue = "27779")]
61+
pub trait Placer<Data: ?Sized> {
62+
/// `Place` is the intermedate agent guarding the
63+
/// uninitialized state for `Data`.
64+
type Place: InPlace<Data>;
65+
66+
/// Creates a fresh place from `self`.
67+
fn make_place(self) -> Self::Place;
68+
}
69+
70+
/// Specialization of `Place` trait supporting `PLACE <- EXPR`.
71+
#[unstable(feature = "placement_new_protocol", issue = "27779")]
72+
pub trait InPlace<Data: ?Sized>: Place<Data> {
73+
/// `Owner` is the type of the end value of `PLACE <- EXPR`
74+
///
75+
/// Note that when `PLACE <- EXPR` is solely used for
76+
/// side-effecting an existing data-structure,
77+
/// e.g. `Vec::emplace_back`, then `Owner` need not carry any
78+
/// information at all (e.g. it can be the unit type `()` in that
79+
/// case).
80+
type Owner;
81+
82+
/// Converts self into the final value, shifting
83+
/// deallocation/cleanup responsibilities (if any remain), over to
84+
/// the returned instance of `Owner` and forgetting self.
85+
unsafe fn finalize(self) -> Self::Owner;
86+
}
87+
88+
/// Core trait for the `box EXPR` form.
89+
///
90+
/// `box EXPR` effectively desugars into:
91+
///
92+
/// ```rust,ignore
93+
/// let mut place = BoxPlace::make_place();
94+
/// let raw_place = Place::pointer(&mut place);
95+
/// let value = EXPR;
96+
/// unsafe {
97+
/// ::std::ptr::write(raw_place, value);
98+
/// Boxed::finalize(place)
99+
/// }
100+
/// ```
101+
///
102+
/// The type of `box EXPR` is supplied from its surrounding
103+
/// context; in the above expansion, the result type `T` is used
104+
/// to determine which implementation of `Boxed` to use, and that
105+
/// `<T as Boxed>` in turn dictates determines which
106+
/// implementation of `BoxPlace` to use, namely:
107+
/// `<<T as Boxed>::Place as BoxPlace>`.
108+
#[unstable(feature = "placement_new_protocol", issue = "27779")]
109+
pub trait Boxed {
110+
/// The kind of data that is stored in this kind of box.
111+
type Data; /* (`Data` unused b/c cannot yet express below bound.) */
112+
/// The place that will negotiate the storage of the data.
113+
type Place: BoxPlace<Self::Data>;
114+
115+
/// Converts filled place into final owning value, shifting
116+
/// deallocation/cleanup responsibilities (if any remain), over to
117+
/// returned instance of `Self` and forgetting `filled`.
118+
unsafe fn finalize(filled: Self::Place) -> Self;
119+
}
120+
121+
/// Specialization of `Place` trait supporting `box EXPR`.
122+
#[unstable(feature = "placement_new_protocol", issue = "27779")]
123+
pub trait BoxPlace<Data: ?Sized> : Place<Data> {
124+
/// Creates a globally fresh place.
125+
fn make_place() -> Self;
126+
}

0 commit comments

Comments
 (0)