core: Remove struct ctors

This commit is contained in:
Brian Anderson 2012-09-04 15:23:28 -07:00
parent ab9cf45a7c
commit 02b1c32e4d
11 changed files with 174 additions and 67 deletions

View File

@ -99,7 +99,6 @@ fn listen<T: send, U>(f: fn(Chan<T>) -> U) -> U {
struct PortPtr<T:send> {
let po: *rust_port;
new(po: *rust_port) { self.po = po; }
drop unsafe {
do task::unkillable {
// Once the port is detached it's guaranteed not to receive further
@ -122,6 +121,12 @@ struct PortPtr<T:send> {
}
}
fn PortPtr<T: send>(po: *rust_port) -> PortPtr<T> {
PortPtr {
po: po
}
}
/**
* Internal function for converting from a channel to a port
*
@ -134,7 +139,6 @@ fn as_raw_port<T: send, U>(ch: comm::Chan<T>, f: fn(*rust_port) -> U) -> U {
struct PortRef {
let p: *rust_port;
new(p: *rust_port) { self.p = p; }
drop {
if !ptr::is_null(self.p) {
rustrt::rust_port_drop(self.p);
@ -142,6 +146,12 @@ fn as_raw_port<T: send, U>(ch: comm::Chan<T>, f: fn(*rust_port) -> U) -> U {
}
}
fn PortRef(p: *rust_port) -> PortRef {
PortRef {
p: p
}
}
let p = PortRef(rustrt::rust_port_take(*ch));
if ptr::is_null(p.p) {

View File

@ -247,10 +247,15 @@ impl<T: Reader, C> {base: T, cleanup: C}: Reader {
struct FILERes {
let f: *libc::FILE;
new(f: *libc::FILE) { self.f = f; }
drop { libc::fclose(self.f); }
}
fn FILERes(f: *libc::FILE) -> FILERes {
FILERes {
f: f
}
}
fn FILE_reader(f: *libc::FILE, cleanup: bool) -> Reader {
if cleanup {
{base: f, cleanup: FILERes(f)} as Reader
@ -417,10 +422,15 @@ impl fd_t: Writer {
struct FdRes {
let fd: fd_t;
new(fd: fd_t) { self.fd = fd; }
drop { libc::close(self.fd); }
}
fn FdRes(fd: fd_t) -> FdRes {
FdRes {
fd: fd
}
}
fn fd_writer(fd: fd_t, cleanup: bool) -> Writer {
if cleanup {
{base: fd, cleanup: FdRes(fd)} as Writer
@ -768,7 +778,6 @@ mod fsync {
// Artifacts that need to fsync on destruction
struct Res<t> {
let arg: Arg<t>;
new(-arg: Arg<t>) { self.arg <- arg; }
drop {
match self.arg.opt_level {
option::None => (),
@ -780,6 +789,12 @@ mod fsync {
}
}
fn Res<t>(-arg: Arg<t>) -> Res<t>{
Res {
arg: move arg
}
}
type Arg<t> = {
val: t,
opt_level: Option<Level>,

View File

@ -292,9 +292,15 @@ fn test_unwrap_str() {
fn test_unwrap_resource() {
struct R {
let i: @mut int;
new(i: @mut int) { self.i = i; }
drop { *(self.i) += 1; }
}
fn R(i: @mut int) -> R {
R {
i: i
}
}
let i = @mut 0;
{
let x = R(i);

View File

@ -135,12 +135,16 @@ struct BufferHeader {
// get away with restricting it to 0 or 1, if we're careful.
let mut ref_count: int;
new() { self.ref_count = 0; }
// We may want a drop, and to be careful about stringing this
// thing along.
}
fn BufferHeader() -> BufferHeader{
BufferHeader {
ref_count: 0
}
}
// XXX remove me
#[cfg(stage0)]
fn buffer_header() -> BufferHeader { BufferHeader() }
@ -160,12 +164,6 @@ struct PacketHeader {
// to a buffer_header if need be.
let mut buffer: *libc::c_void;
new() {
self.state = Empty;
self.blocked_task = ptr::null();
self.buffer = ptr::null();
}
// Returns the old state.
unsafe fn mark_blocked(this: *rust_task) -> State {
rustrt::rust_task_ref(this);
@ -197,6 +195,14 @@ struct PacketHeader {
}
}
fn PacketHeader() -> PacketHeader {
PacketHeader {
state: Empty,
blocked_task: ptr::null(),
buffer: ptr::null()
}
}
#[doc(hidden)]
type Packet<T: send> = {
header: PacketHeader,
@ -368,12 +374,6 @@ unsafe fn get_buffer<T: send>(p: *PacketHeader) -> ~Buffer<T> {
// This could probably be done with SharedMutableState to avoid move_it!().
struct BufferResource<T: send> {
let buffer: ~Buffer<T>;
new(+b: ~Buffer<T>) {
//let p = ptr::addr_of(*b);
//error!("take %?", p);
atomic_add_acq(&mut b.header.ref_count, 1);
self.buffer = b;
}
drop unsafe {
let b = move_it!(self.buffer);
@ -392,6 +392,16 @@ struct BufferResource<T: send> {
}
}
fn BufferResource<T: send>(+b: ~Buffer<T>) -> BufferResource<T> {
//let p = ptr::addr_of(*b);
//error!("take %?", p);
atomic_add_acq(&mut b.header.ref_count, 1);
BufferResource {
buffer: b
}
}
#[doc(hidden)]
fn send<T: send, Tbuffer: send>(+p: SendPacketBuffered<T, Tbuffer>,
+payload: T) -> bool {
@ -770,15 +780,6 @@ fn send_packet<T: send>(p: *packet<T>) -> SendPacket<T> {
struct SendPacketBuffered<T: send, Tbuffer: send> {
let mut p: Option<*Packet<T>>;
let mut buffer: Option<BufferResource<Tbuffer>>;
new(p: *Packet<T>) {
//debug!("take send %?", p);
self.p = Some(p);
unsafe {
self.buffer = Some(
BufferResource(
get_buffer(ptr::addr_of((*p).header))));
};
}
drop {
//if self.p != none {
// debug!("drop send %?", option::get(self.p));
@ -819,6 +820,18 @@ struct SendPacketBuffered<T: send, Tbuffer: send> {
}
}
fn SendPacketBuffered<T: send, Tbuffer: send>(p: *Packet<T>)
-> SendPacketBuffered<T, Tbuffer> {
//debug!("take send %?", p);
SendPacketBuffered {
p: Some(p),
buffer: unsafe {
Some(BufferResource(
get_buffer(ptr::addr_of((*p).header))))
}
}
}
// XXX remove me
#[cfg(stage0)]
#[allow(non_camel_case_types)]
@ -848,15 +861,6 @@ fn recv_packet<T: send>(p: *packet<T>) -> RecvPacket<T> {
struct RecvPacketBuffered<T: send, Tbuffer: send> : Selectable {
let mut p: Option<*Packet<T>>;
let mut buffer: Option<BufferResource<Tbuffer>>;
new(p: *Packet<T>) {
//debug!("take recv %?", p);
self.p = Some(p);
unsafe {
self.buffer = Some(
BufferResource(
get_buffer(ptr::addr_of((*p).header))));
};
}
drop {
//if self.p != none {
// debug!("drop recv %?", option::get(self.p));
@ -897,6 +901,18 @@ struct RecvPacketBuffered<T: send, Tbuffer: send> : Selectable {
}
}
fn RecvPacketBuffered<T: send, Tbuffer: send>(p: *Packet<T>)
-> RecvPacketBuffered<T, Tbuffer> {
//debug!("take recv %?", p);
RecvPacketBuffered {
p: Some(p),
buffer: unsafe {
Some(BufferResource(
get_buffer(ptr::addr_of((*p).header))))
}
}
}
// XXX remove me
#[cfg(stage0)]
#[allow(non_camel_case_types)]
@ -1083,8 +1099,6 @@ impl<T: send> Port<T>: Recv<T> {
struct PortSet<T: send> : Recv<T> {
let mut ports: ~[pipes::Port<T>];
new() { self.ports = ~[]; }
fn add(+port: pipes::Port<T>) {
vec::push(self.ports, port)
}
@ -1131,6 +1145,12 @@ struct PortSet<T: send> : Recv<T> {
}
}
fn PortSet<T: send>() -> PortSet<T>{
PortSet {
ports: ~[]
}
}
impl<T: send> Port<T>: Selectable {
pure fn header() -> *PacketHeader unchecked {
match self.endp {

View File

@ -196,11 +196,16 @@ unsafe fn weaken_task(f: fn(comm::Port<()>)) {
struct Unweaken {
let ch: comm::Chan<()>;
new(ch: comm::Chan<()>) { self.ch = ch; }
drop unsafe {
rustrt::rust_task_unweaken(unsafe::reinterpret_cast(&self.ch));
}
}
fn Unweaken(ch: comm::Chan<()>) -> Unweaken {
Unweaken {
ch: ch
}
}
}
#[test]

View File

@ -245,10 +245,15 @@ impl Rng {
struct RandRes {
let c: *rctx;
new(c: *rctx) { self.c = c; }
drop { rustrt::rand_free(self.c); }
}
fn RandRes(c: *rctx) -> RandRes {
RandRes {
c: c
}
}
impl @RandRes: Rng {
fn next() -> u32 { return rustrt::rand_next((*self).c); }
}

View File

@ -228,10 +228,15 @@ fn start_program(prog: &str, args: &[~str]) -> Program {
}
struct ProgRes {
let r: ProgRepr;
new(+r: ProgRepr) { self.r = r; }
drop { destroy_repr(&self.r); }
}
fn ProgRes(+r: ProgRepr) -> ProgRes {
ProgRes {
r: r
}
}
impl ProgRes: Program {
fn get_id() -> pid_t { return self.r.pid; }
fn input() -> io::Writer { io::fd_writer(self.r.in_fd, false) }

View File

@ -8,9 +8,11 @@ type Word = uint;
struct Frame {
let fp: *Word;
}
new(fp: *Word) {
self.fp = fp;
fn Frame(fp: *Word) -> Frame {
Frame {
fp: fp
}
}

View File

@ -610,10 +610,15 @@ fn get_task() -> Task {
unsafe fn unkillable<U>(f: fn() -> U) -> U {
struct AllowFailure {
let t: *rust_task;
new(t: *rust_task) { self.t = t; }
drop { rustrt::rust_task_allow_kill(self.t); }
}
fn AllowFailure(t: *rust_task) -> AllowFailure{
AllowFailure {
t: t
}
}
let t = rustrt::rust_get_task();
let _allow_failure = AllowFailure(t);
rustrt::rust_task_inhibit_kill(t);
@ -624,10 +629,15 @@ unsafe fn unkillable<U>(f: fn() -> U) -> U {
unsafe fn rekillable<U>(f: fn() -> U) -> U {
struct DisallowFailure {
let t: *rust_task;
new(t: *rust_task) { self.t = t; }
drop { rustrt::rust_task_inhibit_kill(self.t); }
}
fn DisallowFailure(t: *rust_task) -> DisallowFailure {
DisallowFailure {
t: t
}
}
let t = rustrt::rust_get_task();
let _allow_failure = DisallowFailure(t);
rustrt::rust_task_allow_kill(t);
@ -641,12 +651,18 @@ unsafe fn rekillable<U>(f: fn() -> U) -> U {
unsafe fn atomically<U>(f: fn() -> U) -> U {
struct DeferInterrupts {
let t: *rust_task;
new(t: *rust_task) { self.t = t; }
drop {
rustrt::rust_task_allow_yield(self.t);
rustrt::rust_task_allow_kill(self.t);
}
}
fn DeferInterrupts(t: *rust_task) -> DeferInterrupts {
DeferInterrupts {
t: t
}
}
let t = rustrt::rust_get_task();
let _interrupts = DeferInterrupts(t);
rustrt::rust_task_inhibit_kill(t);
@ -938,15 +954,6 @@ struct TCB {
let mut ancestors: AncestorList;
let is_main: bool;
let notifier: Option<AutoNotify>;
new(me: *rust_task, -tasks: TaskGroupArc, -ancestors: AncestorList,
is_main: bool, -notifier: Option<AutoNotify>) {
self.me = me;
self.tasks = tasks;
self.ancestors = ancestors;
self.is_main = is_main;
self.notifier = notifier;
self.notifier.iter(|x| { x.failed = false; });
}
// Runs on task exit.
drop {
// If we are failing, the whole taskgroup needs to die.
@ -971,19 +978,37 @@ struct TCB {
}
}
fn TCB(me: *rust_task, +tasks: TaskGroupArc, +ancestors: AncestorList,
is_main: bool, +notifier: Option<AutoNotify>) -> TCB {
let notifier = move notifier;
notifier.iter(|x| { x.failed = false; });
TCB {
me: me,
tasks: tasks,
ancestors: ancestors,
is_main: is_main,
notifier: move notifier
}
}
struct AutoNotify {
let notify_chan: comm::Chan<Notification>;
let mut failed: bool;
new(chan: comm::Chan<Notification>) {
self.notify_chan = chan;
self.failed = true; // Un-set above when taskgroup successfully made.
}
drop {
let result = if self.failed { Failure } else { Success };
comm::send(self.notify_chan, Exit(get_task(), result));
}
}
fn AutoNotify(chan: comm::Chan<Notification>) -> AutoNotify {
AutoNotify {
notify_chan: chan,
failed: true // Un-set above when taskgroup successfully made.
}
}
fn enlist_in_taskgroup(state: TaskGroupInner, me: *rust_task,
is_member: bool) -> bool {
let newstate = util::replace(state, None);

View File

@ -95,7 +95,6 @@ struct ArcData<T> {
struct ArcDestruct<T> {
mut data: *libc::c_void;
new(data: *libc::c_void) { self.data = data; }
drop unsafe {
if self.data.is_null() {
return; // Happens when destructing an unwrapper's handle.
@ -132,6 +131,12 @@ struct ArcDestruct<T> {
}
}
fn ArcDestruct<T>(data: *libc::c_void) -> ArcDestruct<T> {
ArcDestruct {
data: data
}
}
unsafe fn unwrap_shared_mutable_state<T: send>(+rc: SharedMutableState<T>)
-> T {
struct DeathThroes<T> {
@ -276,21 +281,29 @@ extern mod rustrt {
struct LittleLock {
let l: rust_little_lock;
new() {
self.l = rustrt::rust_create_little_lock();
}
drop { rustrt::rust_destroy_little_lock(self.l); }
}
fn LittleLock() -> LittleLock {
LittleLock {
l: rustrt::rust_create_little_lock()
}
}
impl LittleLock {
#[inline(always)]
unsafe fn lock<T>(f: fn() -> T) -> T {
struct Unlock {
let l: rust_little_lock;
new(l: rust_little_lock) { self.l = l; }
drop { rustrt::rust_unlock_little_lock(self.l); }
}
fn Unlock(l: rust_little_lock) -> Unlock {
Unlock {
l: l
}
}
do atomically {
rustrt::rust_lock_little_lock(self.l);
let _r = Unlock(self.l);

View File

@ -55,10 +55,11 @@ fn replace<T>(dest: &mut T, +src: T) -> T {
/// A non-copyable dummy type.
struct NonCopyable {
i: ();
new() { self.i = (); }
drop { }
}
fn NonCopyable() -> NonCopyable { NonCopyable { i: () } }
mod tests {
#[test]
fn identity_crisis() {