Added Truncate trait (#1141)
This commit is contained in:
parent
44b798c07e
commit
6894a37ceb
@ -180,7 +180,7 @@ pub fn main() {
|
|||||||
.observers_mut()
|
.observers_mut()
|
||||||
.match_name_mut::<HitcountsMapObserver<StdMapObserver<'_, u8, false>>>("shared_mem")
|
.match_name_mut::<HitcountsMapObserver<StdMapObserver<'_, u8, false>>>("shared_mem")
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.downsize_map(dynamic_map_size);
|
.truncate(dynamic_map_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut executor = TimeoutForkserverExecutor::with_signal(
|
let mut executor = TimeoutForkserverExecutor::with_signal(
|
||||||
|
@ -180,7 +180,7 @@ pub fn main() {
|
|||||||
.observers_mut()
|
.observers_mut()
|
||||||
.match_name_mut::<HitcountsMapObserver<StdMapObserver<'_, u8, false>>>("shared_mem")
|
.match_name_mut::<HitcountsMapObserver<StdMapObserver<'_, u8, false>>>("shared_mem")
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.downsize_map(dynamic_map_size);
|
.truncate(dynamic_map_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut executor = TimeoutForkserverExecutor::with_signal(
|
let mut executor = TimeoutForkserverExecutor::with_signal(
|
||||||
|
@ -154,6 +154,12 @@ pub trait HasRefCnt {
|
|||||||
fn refcnt_mut(&mut self) -> &mut isize;
|
fn refcnt_mut(&mut self) -> &mut isize;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Trait to truncate slices and maps to a new size
|
||||||
|
pub trait Truncate {
|
||||||
|
/// Reduce the size of the slice
|
||||||
|
fn truncate(&mut self, len: usize);
|
||||||
|
}
|
||||||
|
|
||||||
/// Current time
|
/// Current time
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
#[must_use]
|
#[must_use]
|
||||||
|
@ -10,7 +10,7 @@ use core::{clone::Clone, fmt::Debug, slice};
|
|||||||
|
|
||||||
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
||||||
|
|
||||||
use crate::bolts::{AsMutSlice, AsSlice};
|
use crate::bolts::{AsMutSlice, AsSlice, Truncate};
|
||||||
|
|
||||||
/// Trait to convert into an Owned type
|
/// Trait to convert into an Owned type
|
||||||
pub trait IntoOwned {
|
pub trait IntoOwned {
|
||||||
@ -23,20 +23,14 @@ pub trait IntoOwned {
|
|||||||
fn into_owned(self) -> Self;
|
fn into_owned(self) -> Self;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Trait to downsize slice references
|
impl<'a, T> Truncate for &'a [T] {
|
||||||
pub trait DownsizeSlice {
|
fn truncate(&mut self, len: usize) {
|
||||||
/// Reduce the size of the slice
|
|
||||||
fn downsize(&mut self, len: usize);
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a, T> DownsizeSlice for &'a [T] {
|
|
||||||
fn downsize(&mut self, len: usize) {
|
|
||||||
*self = &self[..len];
|
*self = &self[..len];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, T> DownsizeSlice for &'a mut [T] {
|
impl<'a, T> Truncate for &'a mut [T] {
|
||||||
fn downsize(&mut self, len: usize) {
|
fn truncate(&mut self, len: usize) {
|
||||||
let mut value = core::mem::take(self);
|
let mut value = core::mem::take(self);
|
||||||
value = unsafe { value.get_unchecked_mut(..len) };
|
value = unsafe { value.get_unchecked_mut(..len) };
|
||||||
let _: &mut [T] = core::mem::replace(self, value);
|
let _: &mut [T] = core::mem::replace(self, value);
|
||||||
@ -260,8 +254,8 @@ impl<'a, T> OwnedSlice<'a, T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Downsize the inner slice or vec returning the old size on success or `None` on failure
|
/// Truncate the inner slice or vec returning the old size on success or `None` on failure
|
||||||
pub fn downsize(&mut self, new_len: usize) -> Option<usize> {
|
pub fn truncate(&mut self, new_len: usize) -> Option<usize> {
|
||||||
match &mut self.inner {
|
match &mut self.inner {
|
||||||
OwnedSliceInner::RefRaw(_rr, len) => {
|
OwnedSliceInner::RefRaw(_rr, len) => {
|
||||||
let tmp = *len;
|
let tmp = *len;
|
||||||
@ -275,7 +269,7 @@ impl<'a, T> OwnedSlice<'a, T> {
|
|||||||
OwnedSliceInner::Ref(r) => {
|
OwnedSliceInner::Ref(r) => {
|
||||||
let tmp = r.len();
|
let tmp = r.len();
|
||||||
if new_len <= tmp {
|
if new_len <= tmp {
|
||||||
r.downsize(new_len);
|
r.truncate(new_len);
|
||||||
Some(tmp)
|
Some(tmp)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
@ -483,8 +477,8 @@ impl<'a, T: 'a + Sized> OwnedMutSlice<'a, T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Downsize the inner slice or vec returning the old size on success or `None` on failure
|
/// Truncate the inner slice or vec returning the old size on success or `None` on failure
|
||||||
pub fn downsize(&mut self, new_len: usize) -> Option<usize> {
|
pub fn truncate(&mut self, new_len: usize) -> Option<usize> {
|
||||||
match &mut self.inner {
|
match &mut self.inner {
|
||||||
OwnedMutSliceInner::RefRaw(_rr, len) => {
|
OwnedMutSliceInner::RefRaw(_rr, len) => {
|
||||||
let tmp = *len;
|
let tmp = *len;
|
||||||
@ -498,7 +492,7 @@ impl<'a, T: 'a + Sized> OwnedMutSlice<'a, T> {
|
|||||||
OwnedMutSliceInner::Ref(r) => {
|
OwnedMutSliceInner::Ref(r) => {
|
||||||
let tmp = r.len();
|
let tmp = r.len();
|
||||||
if new_len <= tmp {
|
if new_len <= tmp {
|
||||||
r.downsize(new_len);
|
r.truncate(new_len);
|
||||||
Some(tmp)
|
Some(tmp)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
|
@ -32,7 +32,7 @@ use crate::{
|
|||||||
os::{dup2, pipes::Pipe},
|
os::{dup2, pipes::Pipe},
|
||||||
shmem::{ShMem, ShMemProvider, UnixShMemProvider},
|
shmem::{ShMem, ShMemProvider, UnixShMemProvider},
|
||||||
tuples::Prepend,
|
tuples::Prepend,
|
||||||
AsMutSlice, AsSlice,
|
AsMutSlice, AsSlice, Truncate,
|
||||||
},
|
},
|
||||||
executors::{Executor, ExitKind, HasObservers},
|
executors::{Executor, ExitKind, HasObservers},
|
||||||
inputs::{HasTargetBytes, Input, UsesInput},
|
inputs::{HasTargetBytes, Input, UsesInput},
|
||||||
@ -653,7 +653,7 @@ impl<'a, SP> ForkserverExecutorBuilder<'a, SP> {
|
|||||||
other_observers: OT,
|
other_observers: OT,
|
||||||
) -> Result<ForkserverExecutor<(MO, OT), S, SP>, Error>
|
) -> Result<ForkserverExecutor<(MO, OT), S, SP>, Error>
|
||||||
where
|
where
|
||||||
MO: Observer<S> + MapObserver, // TODO maybe enforce Entry = u8 for the cov map
|
MO: Observer<S> + MapObserver + Truncate, // TODO maybe enforce Entry = u8 for the cov map
|
||||||
OT: ObserversTuple<S> + Prepend<MO, PreprendResult = OT>,
|
OT: ObserversTuple<S> + Prepend<MO, PreprendResult = OT>,
|
||||||
S: UsesInput,
|
S: UsesInput,
|
||||||
S::Input: Input + HasTargetBytes,
|
S::Input: Input + HasTargetBytes,
|
||||||
@ -671,7 +671,7 @@ impl<'a, SP> ForkserverExecutorBuilder<'a, SP> {
|
|||||||
);
|
);
|
||||||
|
|
||||||
if let Some(dynamic_map_size) = self.map_size {
|
if let Some(dynamic_map_size) = self.map_size {
|
||||||
map_observer.downsize_map(dynamic_map_size);
|
map_observer.truncate(dynamic_map_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
let observers: (MO, OT) = other_observers.prepend(map_observer);
|
let observers: (MO, OT) = other_observers.prepend(map_observer);
|
||||||
|
@ -21,7 +21,7 @@ use crate::{
|
|||||||
bolts::{
|
bolts::{
|
||||||
ownedref::{OwnedMutPtr, OwnedMutSlice},
|
ownedref::{OwnedMutPtr, OwnedMutSlice},
|
||||||
tuples::Named,
|
tuples::Named,
|
||||||
AsIter, AsIterMut, AsMutSlice, AsSlice, HasLen,
|
AsIter, AsIterMut, AsMutSlice, AsSlice, HasLen, Truncate,
|
||||||
},
|
},
|
||||||
executors::ExitKind,
|
executors::ExitKind,
|
||||||
inputs::UsesInput,
|
inputs::UsesInput,
|
||||||
@ -116,12 +116,6 @@ pub trait MapObserver: HasLen + Named + Serialize + serde::de::DeserializeOwned
|
|||||||
|
|
||||||
/// Get the number of set entries with the specified indexes
|
/// Get the number of set entries with the specified indexes
|
||||||
fn how_many_set(&self, indexes: &[usize]) -> usize;
|
fn how_many_set(&self, indexes: &[usize]) -> usize;
|
||||||
|
|
||||||
/// Resize the inner map to be smaller (and thus faster to process)
|
|
||||||
/// It returns Some(old size) on success, None on failure
|
|
||||||
fn downsize_map(&mut self, _new_len: usize) -> Option<usize> {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A Simple iterator calling `MapObserver::get`
|
/// A Simple iterator calling `MapObserver::get`
|
||||||
@ -410,9 +404,21 @@ where
|
|||||||
}
|
}
|
||||||
res
|
res
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn downsize_map(&mut self, new_len: usize) -> Option<usize> {
|
impl<'a, T, const DIFFERENTIAL: bool> Truncate for StdMapObserver<'a, T, DIFFERENTIAL>
|
||||||
self.map.downsize(new_len)
|
where
|
||||||
|
T: Bounded
|
||||||
|
+ PartialEq
|
||||||
|
+ Default
|
||||||
|
+ Copy
|
||||||
|
+ 'static
|
||||||
|
+ Serialize
|
||||||
|
+ serde::de::DeserializeOwned
|
||||||
|
+ Debug,
|
||||||
|
{
|
||||||
|
fn truncate(&mut self, new_len: usize) {
|
||||||
|
self.map.truncate(new_len);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1316,9 +1322,14 @@ where
|
|||||||
fn how_many_set(&self, indexes: &[usize]) -> usize {
|
fn how_many_set(&self, indexes: &[usize]) -> usize {
|
||||||
self.base.how_many_set(indexes)
|
self.base.how_many_set(indexes)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn downsize_map(&mut self, new_len: usize) -> Option<usize> {
|
impl<M> Truncate for HitcountsMapObserver<M>
|
||||||
self.base.downsize_map(new_len)
|
where
|
||||||
|
M: Named + Serialize + serde::de::DeserializeOwned + Truncate,
|
||||||
|
{
|
||||||
|
fn truncate(&mut self, new_len: usize) {
|
||||||
|
self.base.truncate(new_len);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1539,9 +1550,14 @@ where
|
|||||||
fn how_many_set(&self, indexes: &[usize]) -> usize {
|
fn how_many_set(&self, indexes: &[usize]) -> usize {
|
||||||
self.base.how_many_set(indexes)
|
self.base.how_many_set(indexes)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn downsize_map(&mut self, new_len: usize) -> Option<usize> {
|
impl<M> Truncate for HitcountsIterableMapObserver<M>
|
||||||
self.base.downsize_map(new_len)
|
where
|
||||||
|
M: Named + Serialize + serde::de::DeserializeOwned + Truncate,
|
||||||
|
{
|
||||||
|
fn truncate(&mut self, new_len: usize) {
|
||||||
|
self.base.truncate(new_len);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user