Skip to content

Commit

Permalink
Merge branch 'master' into buffer-rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-zen authored Sep 3, 2017
2 parents 50482bd + 46f10d1 commit f915b06
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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, &note_on).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion examples/receive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion examples/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
17 changes: 13 additions & 4 deletions src/endpoints/destinations.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use coremidi_sys::{
MIDIGetNumberOfDestinations, MIDIGetDestination, ItemCount
MIDIGetNumberOfDestinations, MIDIGetDestination, MIDIEndpointDispose, ItemCount
};

use std::ops::Deref;
Expand All @@ -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<Destination> {
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) } })
}
}
}

Expand Down Expand Up @@ -73,7 +76,7 @@ impl Iterator for DestinationsIterator {

fn next(&mut self) -> Option<Destination> {
if self.index < self.count {
let destination = Some(Destination::from_index(self.index));
let destination = Destination::from_index(self.index);
self.index += 1;
destination
}
Expand All @@ -94,3 +97,9 @@ impl Deref for VirtualDestination {
&self.endpoint
}
}

impl Drop for VirtualDestination {
fn drop(&mut self) {
unsafe { MIDIEndpointDispose(self.endpoint.object.0) };
}
}
21 changes: 17 additions & 4 deletions src/endpoints/sources.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<Source> {
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) } })
}
}
}

Expand Down Expand Up @@ -76,7 +83,7 @@ impl Iterator for SourcesIterator {

fn next(&mut self) -> Option<Source> {
if self.index < self.count {
let source = Some(Source::from_index(self.index));
let source = Source::from_index(self.index);
self.index += 1;
source
}
Expand Down Expand Up @@ -106,3 +113,9 @@ impl Deref for VirtualSource {
&self.endpoint
}
}

impl Drop for VirtualSource {
fn drop(&mut self) {
unsafe { MIDIEndpointDispose(self.endpoint.object.0) };
}
}
10 changes: 5 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, &note_on).unwrap();
Expand Down Expand Up @@ -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();
/// ```
Expand All @@ -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)]
Expand All @@ -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());
/// ```
///
Expand All @@ -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());
/// ```
///
Expand Down

0 comments on commit f915b06

Please sign in to comment.