Expose .fail as the opposite of .ok

This commit is contained in:
David Tolnay 2022-07-23 12:51:54 -07:00
parent c47432e38b
commit fb6de5f433
No known key found for this signature in database
GPG key ID: F9BA143B95FF6D82
3 changed files with 23 additions and 5 deletions

View file

@ -47,7 +47,7 @@ pub(crate) unsafe fn unsafe_main(
) -> Result<(), Box<dyn Error>> {
let mut emitter = MaybeUninit::<yaml_emitter_t>::uninit();
let emitter = emitter.as_mut_ptr();
if !yaml_emitter_initialize(emitter).ok {
if yaml_emitter_initialize(emitter).fail {
return Err("Could not initalize the emitter object".into());
}
@ -133,10 +133,10 @@ pub(crate) unsafe fn unsafe_main(
break Err(format!("Unknown event: '{}'", CStr::from_ptr(line)).into());
};
if !result.ok {
if result.fail {
break Err("Memory error: Not enough memory for creating an event".into());
}
if !yaml_emitter_emit(emitter, event).ok {
if yaml_emitter_emit(emitter, event).fail {
break Err(match (*emitter).error {
YAML_MEMORY_ERROR => "Memory error: Not enough memory for emitting".into(),
YAML_WRITER_ERROR => {

View file

@ -40,7 +40,7 @@ pub(crate) unsafe fn unsafe_main(
) -> Result<(), Box<dyn Error>> {
let mut parser = MaybeUninit::<yaml_parser_t>::uninit();
let parser = parser.as_mut_ptr();
if !yaml_parser_initialize(parser).ok {
if yaml_parser_initialize(parser).fail {
return Err("Could not initialize the parser object".into());
}
@ -66,7 +66,7 @@ pub(crate) unsafe fn unsafe_main(
let mut event = MaybeUninit::<yaml_event_t>::uninit();
let event = event.as_mut_ptr();
loop {
if !yaml_parser_parse(parser, event).ok {
if yaml_parser_parse(parser, event).fail {
let mut error = format!("Parse error: {}", CStr::from_ptr((*parser).problem));
if (*parser).problem_mark.line != 0 || (*parser).problem_mark.column != 0 {
let _ = write!(

View file

@ -1,3 +1,5 @@
use core::ops::Deref;
pub const OK: Success = Success { ok: true };
pub const FAIL: Success = Success { ok: false };
@ -6,6 +8,22 @@ pub struct Success {
pub ok: bool,
}
pub struct Failure {
pub fail: bool,
}
impl Deref for Success {
type Target = Failure;
fn deref(&self) -> &Self::Target {
if self.ok {
&Failure { fail: false }
} else {
&Failure { fail: true }
}
}
}
pub struct Zero;
impl PartialEq<Zero> for Success {