Added Truncate trait (#1141)
This commit is contained in:
parent
44b798c07e
commit
6894a37ceb
@ -180,7 +180,7 @@ pub fn main() {
|
||||
.observers_mut()
|
||||
.match_name_mut::<HitcountsMapObserver<StdMapObserver<'_, u8, false>>>("shared_mem")
|
||||
.unwrap()
|
||||
.downsize_map(dynamic_map_size);
|
||||
.truncate(dynamic_map_size);
|
||||
}
|
||||
|
||||
let mut executor = TimeoutForkserverExecutor::with_signal(
|
||||
|
@ -180,7 +180,7 @@ pub fn main() {
|
||||
.observers_mut()
|
||||
.match_name_mut::<HitcountsMapObserver<StdMapObserver<'_, u8, false>>>("shared_mem")
|
||||
.unwrap()
|
||||
.downsize_map(dynamic_map_size);
|
||||
.truncate(dynamic_map_size);
|
||||
}
|
||||
|
||||
let mut executor = TimeoutForkserverExecutor::with_signal(
|
||||
|
@ -154,6 +154,12 @@ pub trait HasRefCnt {
|
||||
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
|
||||
#[cfg(feature = "std")]
|
||||
#[must_use]
|
||||
|
@ -10,7 +10,7 @@ use core::{clone::Clone, fmt::Debug, slice};
|
||||
|
||||
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
||||
|
||||
use crate::bolts::{AsMutSlice, AsSlice};
|
||||
use crate::bolts::{AsMutSlice, AsSlice, Truncate};
|
||||
|
||||
/// Trait to convert into an Owned type
|
||||
pub trait IntoOwned {
|
||||
@ -23,20 +23,14 @@ pub trait IntoOwned {
|
||||
fn into_owned(self) -> Self;
|
||||
}
|
||||
|
||||
/// Trait to downsize slice references
|
||||
pub trait DownsizeSlice {
|
||||
/// 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) {
|
||||
impl<'a, T> Truncate for &'a [T] {
|
||||
fn truncate(&mut self, len: usize) {
|
||||
*self = &self[..len];
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T> DownsizeSlice for &'a mut [T] {
|
||||
fn downsize(&mut self, len: usize) {
|
||||
impl<'a, T> Truncate for &'a mut [T] {
|
||||
fn truncate(&mut self, len: usize) {
|
||||
let mut value = core::mem::take(self);
|
||||
value = unsafe { value.get_unchecked_mut(..len) };
|
||||
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
|
||||
pub fn downsize(&mut self, new_len: usize) -> Option<usize> {
|
||||
/// Truncate the inner slice or vec returning the old size on success or `None` on failure
|
||||
pub fn truncate(&mut self, new_len: usize) -> Option<usize> {
|
||||
match &mut self.inner {
|
||||
OwnedSliceInner::RefRaw(_rr, len) => {
|
||||
let tmp = *len;
|
||||
@ -275,7 +269,7 @@ impl<'a, T> OwnedSlice<'a, T> {
|
||||
OwnedSliceInner::Ref(r) => {
|
||||
let tmp = r.len();
|
||||
if new_len <= tmp {
|
||||
r.downsize(new_len);
|
||||
r.truncate(new_len);
|
||||
Some(tmp)
|
||||
} else {
|
||||
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
|
||||
pub fn downsize(&mut self, new_len: usize) -> Option<usize> {
|
||||
/// Truncate the inner slice or vec returning the old size on success or `None` on failure
|
||||
pub fn truncate(&mut self, new_len: usize) -> Option<usize> {
|
||||
match &mut self.inner {
|
||||
OwnedMutSliceInner::RefRaw(_rr, len) => {
|
||||
let tmp = *len;
|
||||
@ -498,7 +492,7 @@ impl<'a, T: 'a + Sized> OwnedMutSlice<'a, T> {
|
||||
OwnedMutSliceInner::Ref(r) => {
|
||||
let tmp = r.len();
|
||||
if new_len <= tmp {
|
||||
r.downsize(new_len);
|
||||
r.truncate(new_len);
|
||||
Some(tmp)
|
||||
} else {
|
||||
None
|
||||
|
@ -32,7 +32,7 @@ use crate::{
|
||||
os::{dup2, pipes::Pipe},
|
||||
shmem::{ShMem, ShMemProvider, UnixShMemProvider},
|
||||
tuples::Prepend,
|
||||
AsMutSlice, AsSlice,
|
||||
AsMutSlice, AsSlice, Truncate,
|
||||
},
|
||||
executors::{Executor, ExitKind, HasObservers},
|
||||
inputs::{HasTargetBytes, Input, UsesInput},
|
||||
@ -653,7 +653,7 @@ impl<'a, SP> ForkserverExecutorBuilder<'a, SP> {
|
||||
other_observers: OT,
|
||||
) -> Result<ForkserverExecutor<(MO, OT), S, SP>, Error>
|
||||
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>,
|
||||
S: UsesInput,
|
||||
S::Input: Input + HasTargetBytes,
|
||||
@ -671,7 +671,7 @@ impl<'a, SP> ForkserverExecutorBuilder<'a, SP> {
|
||||
);
|
||||
|
||||
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);
|
||||
|
@ -21,7 +21,7 @@ use crate::{
|
||||
bolts::{
|
||||
ownedref::{OwnedMutPtr, OwnedMutSlice},
|
||||
tuples::Named,
|
||||
AsIter, AsIterMut, AsMutSlice, AsSlice, HasLen,
|
||||
AsIter, AsIterMut, AsMutSlice, AsSlice, HasLen, Truncate,
|
||||
},
|
||||
executors::ExitKind,
|
||||
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
|
||||
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`
|
||||
@ -410,9 +404,21 @@ where
|
||||
}
|
||||
res
|
||||
}
|
||||
}
|
||||
|
||||
fn downsize_map(&mut self, new_len: usize) -> Option<usize> {
|
||||
self.map.downsize(new_len)
|
||||
impl<'a, T, const DIFFERENTIAL: bool> Truncate for StdMapObserver<'a, T, DIFFERENTIAL>
|
||||
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 {
|
||||
self.base.how_many_set(indexes)
|
||||
}
|
||||
}
|
||||
|
||||
fn downsize_map(&mut self, new_len: usize) -> Option<usize> {
|
||||
self.base.downsize_map(new_len)
|
||||
impl<M> Truncate for HitcountsMapObserver<M>
|
||||
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 {
|
||||
self.base.how_many_set(indexes)
|
||||
}
|
||||
}
|
||||
|
||||
fn downsize_map(&mut self, new_len: usize) -> Option<usize> {
|
||||
self.base.downsize_map(new_len)
|
||||
impl<M> Truncate for HitcountsIterableMapObserver<M>
|
||||
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