1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use std::mem;
use std::ptr;

use super::Ref;
use ffi::*;
use libc::c_int;

pub struct Borrow<'a> {
    packet: AVPacket,
    data: &'a [u8],
}

impl<'a> Borrow<'a> {
    pub fn new(data: &[u8]) -> Borrow {
        unsafe {
            let mut packet: AVPacket = mem::zeroed();

            packet.data = data.as_ptr() as *mut _;
            packet.size = data.len() as c_int;

            Borrow { packet, data }
        }
    }

    #[inline]
    pub fn size(&self) -> usize {
        self.packet.size as usize
    }

    #[inline]
    pub fn data(&self) -> Option<&[u8]> {
        Some(self.data)
    }
}

impl<'a> Ref for Borrow<'a> {
    fn as_ptr(&self) -> *const AVPacket {
        &self.packet
    }
}

impl<'a> Drop for Borrow<'a> {
    fn drop(&mut self) {
        unsafe {
            self.packet.data = ptr::null_mut();
            self.packet.size = 0;

            av_packet_unref(&mut self.packet);
        }
    }
}