use ibverbs_sys::ibv_access_flags;
#[derive(Debug, Copy, Clone)]
pub struct AccessFlags(u32);
impl AccessFlags {
pub fn new() -> Self {
Self(0)
}
pub fn with_local_write(mut self) -> Self {
self.0 |= ibv_access_flags::IBV_ACCESS_LOCAL_WRITE.0;
self
}
pub fn with_remote_read(mut self) -> Self {
self.0 |= ibv_access_flags::IBV_ACCESS_REMOTE_READ.0;
self
}
pub fn with_remote_write(mut self) -> Self {
self.0 |= ibv_access_flags::IBV_ACCESS_REMOTE_WRITE.0;
self
}
pub fn code(&self) -> u32 {
self.0
}
}
impl Default for AccessFlags {
fn default() -> AccessFlags {
AccessFlags::new().with_local_write()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn new_starts_empty() {
assert_eq!(AccessFlags::new().code(), 0);
}
#[test]
fn local_write_flag() {
let flags = AccessFlags::new().with_local_write();
assert_eq!(flags.code(), 1);
}
#[test]
fn remote_write_flag() {
let flags = AccessFlags::new().with_remote_write();
assert_eq!(flags.code(), 2);
}
#[test]
fn remote_read_flag() {
let flags = AccessFlags::new().with_remote_read();
assert_eq!(flags.code(), 4);
}
#[test]
fn all_flags_combined() {
let flags = AccessFlags::new()
.with_local_write()
.with_remote_write()
.with_remote_read();
assert_eq!(flags.code(), 1 | 2 | 4);
}
#[test]
fn default_is_local_write() {
assert_eq!(
AccessFlags::default().code(),
AccessFlags::new().with_local_write().code()
);
}
#[test]
fn applying_same_flag_twice_is_idempotent() {
let once = AccessFlags::new().with_local_write();
let twice = AccessFlags::new().with_local_write().with_local_write();
assert_eq!(once.code(), twice.code());
}
#[test]
fn order_does_not_matter() {
let a = AccessFlags::new()
.with_local_write()
.with_remote_read()
.with_remote_write();
let b = AccessFlags::new()
.with_remote_write()
.with_local_write()
.with_remote_read();
assert_eq!(a.code(), b.code());
}
}