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

Deprecated

See combination function for reason of deprecation

Similar to safe combination function except the way it return the combination. 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

  • domain A raw data to get combination.
  • r A size of each combination.
  • result A mutable pointer to slice of length equals to r
  • 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 combination 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 combination from single source of data then distribute the combination to two workers which read each combination then do something about it, which in this example, simply print it.

   use permutator::unsafe_combination;
   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 combination
   }
 
   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 combination
   }
 
   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_combination_process<'a>(data : &'a[i32], cur_result : *mut [&'a i32], k : usize, consumers : Vec<Box<Consumer + 'a>>) {
       unsafe_combination(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_combination_process(data, shared, k, consumers);
   }

See