On 11/30/13 8:06 PM, Brendan Zabarauskas wrote:

On 1 Dec 2013, at 1:45 pm, Patrick Walton <[email protected]> wrote:

the experiment of using blocks in place of RAII for stuff like "with_c_str" or 
"unkillable" has failed: it leads to too much rightward drift.

Patrick

I guess I can agree with that. I remember when I first started with Rust doing: 
`some_c_func(“hi”.with_c_str(|s| s))`, which completely negates the safety 
guarantees of the closure. How would this look with RAII?

Just return a package consisting of either an owned string (if allocation needed to be done) or a borrowed pointer to the original string (if no allocation needed to be done). Then you can put a method on that enum yielding the raw C pointer. Voila, no rightward drift :)

By ‘rightward drift’ do you mean in terms of multiple nested closures?

Right. I've seen "with_c_str" six levels deep :(

RAII also has the advantage that you can more precisely control the scope of the effect that you're delimiting: you can move it around, destruct it early, and so on. For example, an RAII'd version of "task::unkillable" would not only have no rightward drift, but the token that makes you killable again could also be destructed on some code paths but not others, moved to other functions, etc.

Patrick

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

Reply via email to