diff --git a/README.md b/README.md index 7917dec06..ba73b0707 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ use std::time::Duration; use std::thread; let client = Client::new("example-client").unwrap(); let output_port = client.output_port("example-port").unwrap(); -let destination = Destinations::from_index(0); +let destination = Destinations::from_index(0).unwrap(); let note_on = PacketBuffer::from_data(0, vec![0x90, 0x40, 0x7f]); let note_off = PacketBuffer::from_data(0, vec![0x80, 0x40, 0x7f]); output_port.send(&destination, ¬e_on).unwrap(); diff --git a/examples/receive.rs b/examples/receive.rs index d73b91404..d11c49021 100644 --- a/examples/receive.rs +++ b/examples/receive.rs @@ -6,7 +6,7 @@ fn main() { let source_index = get_source_index(); println!("Source index: {}", source_index); - let source = coremidi::Source::from_index(source_index); + let source = coremidi::Source::from_index(source_index).unwrap(); println!("Source display name: {}", source.display_name().unwrap()); let client = coremidi::Client::new("example-client").unwrap(); diff --git a/examples/send.rs b/examples/send.rs index 4ba8984f6..3d4e00204 100644 --- a/examples/send.rs +++ b/examples/send.rs @@ -8,7 +8,7 @@ fn main() { let destination_index = get_destination_index(); println!("Destination index: {}", destination_index); - let destination = coremidi::Destination::from_index(destination_index); + let destination = coremidi::Destination::from_index(destination_index).unwrap(); println!("Destination display name: {}", destination.display_name().unwrap()); let client = coremidi::Client::new("example-client").unwrap(); diff --git a/src/endpoints/destinations.rs b/src/endpoints/destinations.rs index b8100ce32..3e342af9c 100644 --- a/src/endpoints/destinations.rs +++ b/src/endpoints/destinations.rs @@ -1,5 +1,5 @@ use coremidi_sys::{ - MIDIGetNumberOfDestinations, MIDIGetDestination, ItemCount + MIDIGetNumberOfDestinations, MIDIGetDestination, MIDIEndpointDispose, ItemCount }; use std::ops::Deref; @@ -13,9 +13,12 @@ impl Destination { /// Create a destination endpoint from its index. /// See [MIDIGetDestination](https://developer.apple.com/reference/coremidi/1495108-midigetdestination) /// - pub fn from_index(index: usize) -> Destination { + pub fn from_index(index: usize) -> Option { let endpoint_ref = unsafe { MIDIGetDestination(index as ItemCount) }; - Destination { endpoint: Endpoint { object: Object(endpoint_ref) } } + match endpoint_ref { + 0 => None, + _ => Some(Destination { endpoint: Endpoint { object: Object(endpoint_ref) } }) + } } } @@ -73,7 +76,7 @@ impl Iterator for DestinationsIterator { fn next(&mut self) -> Option { if self.index < self.count { - let destination = Some(Destination::from_index(self.index)); + let destination = Destination::from_index(self.index); self.index += 1; destination } @@ -94,3 +97,9 @@ impl Deref for VirtualDestination { &self.endpoint } } + +impl Drop for VirtualDestination { + fn drop(&mut self) { + unsafe { MIDIEndpointDispose(self.endpoint.object.0) }; + } +} diff --git a/src/endpoints/sources.rs b/src/endpoints/sources.rs index addacec36..a42e65b6e 100644 --- a/src/endpoints/sources.rs +++ b/src/endpoints/sources.rs @@ -1,7 +1,11 @@ use core_foundation_sys::base::OSStatus; use coremidi_sys::{ - MIDIGetNumberOfSources, MIDIGetSource, MIDIReceived, ItemCount + MIDIGetNumberOfSources, MIDIGetSource, MIDIEndpointDispose, ItemCount +}; + +use coremidi_sys_ext::{ + MIDIReceived }; use std::ops::Deref; @@ -16,9 +20,12 @@ impl Source { /// Create a source endpoint from its index. /// See [MIDIGetSource](https://developer.apple.com/reference/coremidi/1495168-midigetsource) /// - pub fn from_index(index: usize) -> Source { + pub fn from_index(index: usize) -> Option { let endpoint_ref = unsafe { MIDIGetSource(index as ItemCount) }; - Source { endpoint: Endpoint { object: Object(endpoint_ref) } } + match endpoint_ref { + 0 => None, + _ => Some(Source { endpoint: Endpoint { object: Object(endpoint_ref) } }) + } } } @@ -76,7 +83,7 @@ impl Iterator for SourcesIterator { fn next(&mut self) -> Option { if self.index < self.count { - let source = Some(Source::from_index(self.index)); + let source = Source::from_index(self.index); self.index += 1; source } @@ -106,3 +113,9 @@ impl Deref for VirtualSource { &self.endpoint } } + +impl Drop for VirtualSource { + fn drop(&mut self) { + unsafe { MIDIEndpointDispose(self.endpoint.object.0) }; + } +} diff --git a/src/lib.rs b/src/lib.rs index cd8a82d1e..77f724965 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,7 +16,7 @@ use std::time::Duration; use std::thread; let client = coremidi::Client::new("example-client").unwrap(); let output_port = client.output_port("example-port").unwrap(); -let destination = coremidi::Destination::from_index(0); +let destination = coremidi::Destination::from_index(0).unwrap(); let note_on = coremidi::PacketBuffer::new(0, &[0x90, 0x40, 0x7f]); let note_off = coremidi::PacketBuffer::new(0, &[0x80, 0x40, 0x7f]); output_port.send(&destination, ¬e_on).unwrap(); @@ -120,7 +120,7 @@ pub struct Port { object: Object } /// ```rust,no_run /// let client = coremidi::Client::new("example-client").unwrap(); /// let output_port = client.output_port("example-port").unwrap(); -/// let destination = coremidi::Destination::from_index(0); +/// let destination = coremidi::Destination::from_index(0).unwrap(); /// let packets = coremidi::PacketBuffer::new(0, &[0x90, 0x40, 0x7f]); /// output_port.send(&destination, &packets).unwrap(); /// ``` @@ -134,7 +134,7 @@ pub struct OutputPort { port: Port } /// ```rust,no_run /// let client = coremidi::Client::new("example-client").unwrap(); /// let input_port = client.input_port("example-port", |packet_list| println!("{}", packet_list)).unwrap(); -/// let source = coremidi::Source::from_index(0); +/// let source = coremidi::Source::from_index(0).unwrap(); /// input_port.connect_source(&source); /// ``` #[derive(Debug)] @@ -157,7 +157,7 @@ pub struct Endpoint { object: Object } /// A source can be created from an index like this: /// /// ```rust,no_run -/// let source = coremidi::Destination::from_index(0); +/// let source = coremidi::Destination::from_index(0).unwrap(); /// println!("The source at index 0 has display name '{}'", source.display_name().unwrap()); /// ``` /// @@ -169,7 +169,7 @@ pub struct Destination { endpoint: Endpoint } /// A source can be created from an index like this: /// /// ```rust,no_run -/// let source = coremidi::Source::from_index(0); +/// let source = coremidi::Source::from_index(0).unwrap(); /// println!("The source at index 0 has display name '{}'", source.display_name().unwrap()); /// ``` ///