water 0.7.22-alpha

A Rust library that provides a thread-safe distributed message sending facility supporting synchronous and asynchronous I/O across process and machine boundaries. It also uses nets which allow message broadcasts to all, groups, or specific endpoints which eliminates the need for tons of individual channels to interconnect threads. It also provides inter-process communication using a more restricted raw message type.
use std::sync::Mutex;
use std::mem::transmute;
use std::rt::heap::deallocate;
use std::mem::size_of;
use std::mem::align_of;
use std::sync::MutexGuard;
use std::ptr;

/*
struct Internal {
    a:  uint,
    b:  uint,
}


impl Drop for Internal {
    fn drop(&mut self) {
        println!("dropped");
    }
}

fn main() {
    let m = AllocMutex::new(Internal {
        a: 0,
        b: 0,
    });
    
    m.lock().a = 3;
    m.lock().b = 3;
    
    // optional (leak memory)
    m.free();
}
*/

pub struct AllocMutex<T: Send> {
    m:  Mutex<*mut T>,
}

unsafe impl<T: Send + 'static> Send for AllocMutex<T> { }

impl<T: Send> AllocMutex<T> {
    pub fn new(t: T) -> AllocMutex<T> {
        let r: *mut T = unsafe { transmute(box t) };
    
        AllocMutex {
            m:  Mutex::new(r),
        }
    }
    
    pub fn lock(&self) -> MutexGuard<&mut T> {
        unsafe { transmute( self.m.lock() ) }
    }

    pub fn rawmutref(&self) -> *mut T {
        *self.m.lock()
    }
    
    pub fn mutref(&self) -> &mut T {
        unsafe { transmute( (*self.m.lock()) ) }
    }
    
    pub fn free(&self) {
        unsafe {
            let o: *mut T = transmute(self.mutref());
            drop(ptr::read(&(*o)));
            deallocate(o as *mut u8, size_of::<T>(), align_of::<T>());
        }
    }
}