1. 30
    1. 1

      The issue of this approach (and the one proposed on the linked post) is that it leads to a lot of false positive. Any time the Drop implementation of the UseOnce is reachable pre-optimization, the error gets triggered. For example, any of the two lines of code below triggers an error while there is no value that should have been used (none has been created !).

      fn main() {
          //let v : Vec<UseOnce<u8>> = Vec::new();
          let o : Option<UseOnce<u8>> = None;
      }
      

      You may argue that this is acceptable because linearity should be contaminating and the Option/Vec must be used but then this one should clearly be ok and it fails without any real way to fix it.

      pub fn f(v: Vec<UseOnce<u8>>) {
          for x in v {
              x.consume(|_x| ())
          }
      }
      

      There are also multiple ways to leak the UseOnce which would not trigger the error, std::mem::leak, Box::leak or simply putting it in a static.

      1. 1

        How does this handle stack unwinding from panics?

        Edit: didn’t read far enough apparently.