Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not operator overload for SortOptions #3727

Merged
merged 4 commits into from
Feb 16, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions arrow-schema/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ mod field;
pub use field::*;
mod schema;
pub use schema::*;
use std::ops;

#[cfg(feature = "ffi")]
pub mod ffi;
Expand All @@ -47,3 +48,50 @@ impl Default for SortOptions {
}
}
}

/// `!` operator is overloaded for `SortOptions` to invert boolean
/// fields of the struct.
impl ops::Not for SortOptions {
type Output = SortOptions;

fn not(self) -> SortOptions {
SortOptions {
descending: !self.descending,
nulls_first: !self.nulls_first,
}
}
}

#[test]
fn test_overloaded_not_sort_options() {
let sort_options_array = [
SortOptions {
descending: false,
nulls_first: false,
},
SortOptions {
descending: false,
nulls_first: true,
},
SortOptions {
descending: true,
nulls_first: false,
},
SortOptions {
descending: true,
nulls_first: true,
},
];

assert!((!sort_options_array[0]).descending);
assert!((!sort_options_array[0]).nulls_first);

assert!((!sort_options_array[1]).descending);
assert!(!(!sort_options_array[1]).nulls_first);

assert!(!(!sort_options_array[2]).descending);
assert!((!sort_options_array[2]).nulls_first);

assert!(!(!sort_options_array[3]).descending);
assert!(!(!sort_options_array[3]).nulls_first);
}