pub unsafe fn unsafe_k_permutation<'a, T>(
    d: &'a [T],
    k: usize,
    result: *mut [&'a T],
    cb: impl FnMut()
)
Expand description

Similar to safe k_permutation function except the way it return the permutation. It return result through mutable pointer to result assuming the pointer is valid. It’ll notify caller on each new result via empty callback function.

Parameters

  • d A raw data to get k-permutation.
  • k A size of each permutation.
  • result A mutable pointer to slice of length equals to k
  • cb A callback function which will be called after new combination in result is set.

Return

This function return result through function’s parameter result and notify caller that new result is available through cb callback function.

Unsafe

This function is unsafe because it may dereference a dangling pointer, may cause data race if multiple threads read/write to the same memory, and all of those unsafe Rust condition will be applied here.

Rationale

The safe k_permutation function return value in callback parameter. It limit the lifetime of return combination to be valid only inside it callback. To use it outside callback scope, it need to copy the value which will have performance penalty. Therefore, jeopardize it own goal of being fast. This function provide alternative way that sacrifice safety for performance.

Example

The scenario is we want to get k-permutation from single source of data then distribute the permutation to two workers which read each permutation then do something about it, which in this example, simply print it.

   use permutator::unsafe_k_permutation;
   use std::fmt::Debug;
   // define a trait that represent a data consumer
   trait Consumer {
       fn consume(&self); // cannot mut data because rule of no more than 1 ref mut at a time.
   }
 
   struct Worker1<'a, T : 'a> {
       data : &'a[&'a T] // A reference to each k-permutation
   }
 
   impl<'a, T : 'a + Debug> Consumer for Worker1<'a, T> {
       fn consume(&self) {
           // Read data then do something about it. In this case, simply print it.
           println!("Work1 has {:?}", self.data); 
       }
   }
 
   struct Worker2<'a, T : 'a> {
       data : &'a[&'a T] // A reference to each k-permutation
   }
 
   impl<'a, T : 'a + Debug> Consumer for Worker2<'a, T> {
       fn consume(&self) {
           // Read data then do something about it. In this case, simply print it.
           println!("Work2 has {:?}", self.data);
       }
   }

   unsafe fn start_k_permutation_process<'a>(data : &'a[i32], cur_result : *mut [&'a i32], k : usize, consumers : Vec<Box<Consumer + 'a>>) {
       unsafe_k_permutation(data, k, cur_result, || {
           consumers.iter().for_each(|c| {
               c.consume();
           })
       });
   }
   let k = 3;
   let data = &[1, 2, 3, 4, 5];
   let mut result = vec![&data[0]; k];

   unsafe {

       let shared = result.as_mut_slice() as *mut [&i32];
       let worker1 = Worker1 {
           data : &result
       };
       let worker2 = Worker2 {
           data : &result
       };
       let consumers : Vec<Box<Consumer>> = vec![Box::new(worker1), Box::new(worker2)];
       start_k_permutation_process(data, shared, k, consumers);
   }

Note

Performancewise, k_permutation is faster than unsafe_k_permutation. The unsafe function is only faster when caller need to clone the result.

See