#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, Hash)]
#[non_exhaustive]
pub enum RetentionPolicy {
#[default]
Aggressive,
Bounded {
max_bytes: usize,
},
ReleaseAll,
}
impl RetentionPolicy {
pub(crate) const fn allows(self, retained: usize) -> bool {
match self {
Self::Aggressive => true,
Self::Bounded { max_bytes } => retained <= max_bytes,
Self::ReleaseAll => retained == 0,
}
}
}
#[cfg(test)]
mod tests {
use super::RetentionPolicy;
#[test]
fn policies_bound_retained_memory() {
assert!(RetentionPolicy::Aggressive.allows(usize::MAX));
assert!(RetentionPolicy::Bounded { max_bytes: 10 }.allows(10));
assert!(!RetentionPolicy::Bounded { max_bytes: 10 }.allows(11));
assert!(RetentionPolicy::ReleaseAll.allows(0));
assert!(!RetentionPolicy::ReleaseAll.allows(1));
}
}