On 5/1/12 8:02 PM, Brian Anderson wrote:
3) We can come up with a public API to create global, singleton tasks, possibly 
with the following signatures:

     unsafe fn register_named_service<T>(n: str, f: fn~(port<T>))
     unsafe fn get_named_service<T>(n: str) ->  chan<T>

This would, if the named service doesn't exist, create a new task and execute 
the specified function. You would use this in each test to create or retrieve a 
global service to manage your state. The big reservation I have about this is 
that we can not currently make this interface type safe - we can't even check 
the types at runtime.

So, brson and I were discussing this earlier. This was the most type safe pattern I could come up with (it's still flawed):

    enum named_service_key<T> = {
        _named_service_key(str) // not exported
    }
    unsafe fn create_named_service_key<T>(n: str) -> named_service_key<T> {
        _named_service_key(n)
    }

    fn register_named_service<T>(n: named_service_key<T>, f: fn~(port<T>))
    fn get_named_service<T>(n: named_service_key<T>) -> chan<T>

then to create a named service you would do something like:

    fn random_int_service_key() -> named_service_key<()> {
        unsafe{create_named_service_key::<()>("random")}
    }

Now at least the types are not provided anew at each call to `get_named_service()` but rather they are specified when the named service key is created. I use an unsafe fn to make the key itself since that's the point where the type of channels is specified and that will ultimately be unchecked.

But this all seems clumsy.


Niko
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to