[rust-dev] Making fail!() and assert!() usage nicer

Marvin Löbel loebel.marvin at gmail.com
Wed Apr 24 01:53:34 PDT 2013

Am 24.04.2013 04:36, schrieb Brian Anderson:
> On 04/23/2013 03:24 PM, Marvin Löbel wrote:
>> Ever since fail and assert got turned into macros, failing with a 
>> string message has become quite line-noisy:
>> fail!(~"cause")
>> Personally I think 3 is the easiest to implement option, and nice 
>> even without the fmt! optimisation. I already locally implemented the 
>> necessary changes that would allow switching to it after an snapshot, 
>> but I wanted to ask what others think.
> From what you've presented, I like 3.
> I agree with this sentiment in general, and agree that all these 
> related string-taking macros, assert, fail, debug, etc. should behave 
> similarly, generally accepting whatever you throw at them. It would be 
> very desirable I think to not duplicate the string in the case where 
> ~str is the only argument. This could probably be done with a trait 
> that converts to ~str by value (so ~str.to_str_by_val() is a no-op).
> We have always had this lingering notion that fail *could* accept a 
> dynamically typed twiddle, called `~Any`. If we go this direction of 
> passing everything through `fmt!` then that becomes more complicated.
> I think there will be solutions though. In the case where `fail` is 
> passed just one argument it could be interpreted as an instance of 
> some trait, Exceptiony, that can be called to cast the value to 
> `~Any`. When multiple arguments are passed it delegates to fmt.
> _______________________________________________
> Rust-dev mailing list
> Rust-dev at mozilla.org
> https://mail.mozilla.org/listinfo/rust-dev
Ah, didn't even think of the trait solution, that would probably be 
another way to achieve 4. (But it wouldn't be able to accept both 
&'statc str as no copy, and every other &str as a copy)
In fact, there is already a ToStrConsume trait (currently still stuck in 
an pull request) that could be used for that, though it's currently 
blocked on rustc not crashing with owned self. I could just implement 
the macros it as a copy with a call to to_str() for now, and once that 
bug is sorted out change it to to_str_consume() if not accepting a 
non-'static str directly is acceptable. Actually, seeing as that would 
just shift the need to copy a slice into user code, I think it wouldn't 
even be that bad, and more obvious about what's going on.

More information about the Rust-dev mailing list