Skip to content

Commit 7c4135e

Browse files
committed
Reject Unowned in register_port instead of panicking
1 parent 7c19b93 commit 7c4135e

4 files changed

Lines changed: 24 additions & 5 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
- Added `Error::PortUnregistrationError`.
77
- `Client::unregister_port` now returns `Err(Error::PortUnregistrationError)` on failure instead
88
of `Err(Error::PortDisconnectionError)`.
9+
- `Client::register_port` with the `Unowned` spec now returns `Err(Error::PortRegistrationError)`.
10+
- `Unowned::jack_buffer_size` now returns `0` instead of panicking.
911

1012
## 0.9.0
1113

‎src/client/client_impl.rs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ impl Client {
273273
/// Each port has a short name. The port's full name contains the name of the client
274274
/// concatenated with a colon (:) followed by its short name. `Port::name_size()` is the maximum
275275
/// length of the full name. Exceeding that will cause the port registration to fail and return
276-
/// `Err(())`.
276+
/// `Err(Error::PortRegistrationError)`.
277277
///
278278
/// The `port_name` must be unique among all ports owned by this client. If the name is not
279279
/// unique, the registration will fail.
@@ -282,6 +282,10 @@ impl Client {
282282
port_name: &str,
283283
port_spec: PS,
284284
) -> Result<Port<PS>, Error> {
285+
// A port needs a type. This rejects `Unowned`, which only describes existing ports.
286+
if port_spec.jack_port_type().is_empty() {
287+
return Err(Error::PortRegistrationError(port_name.to_string()));
288+
}
285289
let port_name_c = ffi::CString::new(port_name).unwrap();
286290
let port_type_c = ffi::CString::new(port_spec.jack_port_type()).unwrap();
287291
let port_flags = port_spec.jack_flags().bits();

‎src/port/port_impl.rs‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -343,23 +343,27 @@ impl<PS> Port<PS> {
343343

344344
/// `PortSpec` for a port that holds has no readable or writeable data from JACK on the created
345345
/// client. It can be used to connect ports or to obtain metadata.
346+
///
347+
/// `Unowned` describes ports obtained with `Client::port_by_name` or `Client::port_by_id`. It
348+
/// cannot be used to register a port: `Client::register_port` returns
349+
/// `Err(Error::PortRegistrationError)`.
346350
#[derive(Debug, Default)]
347351
pub struct Unowned;
348352

349353
unsafe impl PortSpec for Unowned {
350-
/// Panics on call since the `Unowned` spec can't be used to create ports.
354+
/// Empty, since the `Unowned` spec can't be used to create ports.
351355
fn jack_port_type(&self) -> &str {
352356
""
353357
}
354358

355-
/// Panics on call since the `Unowned` spec can't be used to create ports.
359+
/// Empty, since the `Unowned` spec can't be used to create ports.
356360
fn jack_flags(&self) -> PortFlags {
357361
PortFlags::empty()
358362
}
359363

360-
/// Panics on call since the `Unowned` spec can't be used to create ports.
364+
/// Zero, since the `Unowned` spec can't be used to create ports.
361365
fn jack_buffer_size(&self) -> libc::c_ulong {
362-
unreachable!()
366+
0
363367
}
364368
}
365369

‎src/tests/client.rs‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,12 @@ fn unregistering_another_clients_port_is_an_unregistration_error() {
101101
Err(crate::Error::PortUnregistrationError)
102102
);
103103
}
104+
105+
#[test]
106+
fn registering_an_unowned_port_is_an_error() {
107+
let (client, _) = crate::Client::new("", crate::ClientOptions::default()).unwrap();
108+
assert_eq!(
109+
client.register_port("p", crate::Unowned).map(|_| ()),
110+
Err(crate::Error::PortRegistrationError("p".to_string()))
111+
);
112+
}

0 commit comments

Comments
 (0)