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
use core_foundation_sys::base::OSStatus;
use std::ops::Deref;

use coremidi_sys::{MIDIEndpointRef, MIDIFlushOutput};

use crate::object::Object;

/// A MIDI source or source, owned by an entity.
/// See [MIDIEndpointRef](https://developer.apple.com/documentation/coremidi/midiendpointref).
///
/// You don't need to create an endpoint directly, instead you can create system sources or virtual ones from a client.
///
#[derive(Debug, Hash, Eq, PartialEq)]
pub struct Endpoint {
    pub(crate) object: Object,
}

impl Endpoint {
    pub(crate) fn new(endpoint_ref: MIDIEndpointRef) -> Self {
        Self {
            object: Object(endpoint_ref),
        }
    }

    /// Unschedules previously-sent packets.
    /// See [MIDIFlushOutput](https://developer.apple.com/documentation/coremidi/1495312-midiflushoutput).
    ///
    pub fn flush(&self) -> Result<(), OSStatus> {
        let status = unsafe { MIDIFlushOutput(self.object.0) };
        if status == 0 {
            Ok(())
        } else {
            Err(status)
        }
    }
}

impl AsRef<Object> for Endpoint {
    fn as_ref(&self) -> &Object {
        &self.object
    }
}

impl Deref for Endpoint {
    type Target = Object;

    fn deref(&self) -> &Object {
        &self.object
    }
}