Skip to content

Commit 70be759

Browse files
committed
Allow Error to take any message with ToString
1 parent 39fd968 commit 70be759

14 files changed

+36
-67
lines changed

src/android/l2cap_channel.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -151,13 +151,13 @@ impl L2capChannelReader {
151151
.stream
152152
.recv()
153153
.await
154-
.map_err(|_| Error::new(ErrorKind::ConnectionFailed, None, "channel is closed".to_string()))?;
154+
.map_err(|_| Error::new(ErrorKind::ConnectionFailed, None, "channel is closed"))?;
155155

156156
if packet.len() > buf.len() {
157157
return Err(Error::new(
158158
ErrorKind::InvalidParameter,
159159
None,
160-
"Buffer is too small".to_string(),
160+
"Buffer is too small",
161161
));
162162
}
163163

@@ -169,15 +169,15 @@ impl L2capChannelReader {
169169
#[inline]
170170
pub fn try_read(&mut self, buf: &mut [u8]) -> Result<usize> {
171171
let packet = self.stream.try_recv().map_err(|e| match e {
172-
TryRecvError::Empty => Error::new(ErrorKind::NotReady, None, "no received packet in queue".to_string()),
173-
TryRecvError::Closed => Error::new(ErrorKind::ConnectionFailed, None, "channel is closed".to_string()),
172+
TryRecvError::Empty => Error::new(ErrorKind::NotReady, None, "no received packet in queue"),
173+
TryRecvError::Closed => Error::new(ErrorKind::ConnectionFailed, None, "channel is closed"),
174174
})?;
175175

176176
if packet.len() > buf.len() {
177177
return Err(Error::new(
178178
ErrorKind::InvalidParameter,
179179
None,
180-
"Buffer is too small".to_string(),
180+
"Buffer is too small",
181181
));
182182
}
183183

@@ -208,13 +208,13 @@ impl L2capChannelWriter {
208208
self.stream
209209
.send(packet.to_vec())
210210
.await
211-
.map_err(|_| Error::new(ErrorKind::ConnectionFailed, None, "channel is closed".to_string()))
211+
.map_err(|_| Error::new(ErrorKind::ConnectionFailed, None, "channel is closed"))
212212
}
213213

214214
pub fn try_write(&mut self, packet: &[u8]) -> Result<()> {
215215
self.stream.try_send(packet.to_vec()).map_err(|e| match e {
216-
TrySendError::Closed(_) => Error::new(ErrorKind::ConnectionFailed, None, "channel is closed".to_string()),
217-
TrySendError::Full(_) => Error::new(ErrorKind::NotReady, None, "No buffer space for write".to_string()),
216+
TrySendError::Closed(_) => Error::new(ErrorKind::ConnectionFailed, None, "channel is closed"),
217+
TrySendError::Full(_) => Error::new(ErrorKind::NotReady, None, "No buffer space for write"),
218218
})
219219
}
220220

src/android/mod.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,6 @@ trait OptionExt<T> {
5959
impl<T> OptionExt<T> for Option<T> {
6060
#[track_caller]
6161
fn non_null(self) -> Result<T, crate::Error> {
62-
self.ok_or_else(|| {
63-
crate::Error::new(
64-
ErrorKind::Internal,
65-
None,
66-
"Java call unexpectedly returned null.".to_string(),
67-
)
68-
})
62+
self.ok_or_else(|| crate::Error::new(ErrorKind::Internal, None, "Java call unexpectedly returned null."))
6963
}
7064
}

src/bluer/adapter.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,7 @@ impl AdapterImpl {
6262
.skip_while(|x| x.is_ok() && !matches!(x, Ok(AdapterEvent::Available)))
6363
.next()
6464
.await
65-
.ok_or_else(|| {
66-
Error::new(
67-
ErrorKind::Internal,
68-
None,
69-
"adapter event stream closed unexpectedly".to_string(),
70-
)
71-
})??;
65+
.ok_or_else(|| Error::new(ErrorKind::Internal, None, "adapter event stream closed unexpectedly"))??;
7266
}
7367
Ok(())
7468
}

src/corebluetooth/adapter.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ impl AdapterImpl {
111111
Error::new(
112112
ErrorKind::Internal,
113113
None,
114-
"adapter event stream closed unexpectedly".to_string(),
114+
"adapter event stream closed unexpectedly",
115115
)
116116
})??;
117117
}
@@ -125,7 +125,7 @@ impl AdapterImpl {
125125
peripherals
126126
.first_object()
127127
.map(|x| Device::new(unsafe { ShareId::from_ptr(x as *const _ as *mut _) }))
128-
.ok_or_else(|| Error::new(ErrorKind::NotFound, None, "opening device".to_string()))
128+
.ok_or_else(|| Error::new(ErrorKind::NotFound, None, "opening device"))
129129
}
130130

131131
/// Finds all connected Bluetooth LE devices

src/corebluetooth/characteristic.rs

+5-14
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,10 @@ impl CharacteristicImpl {
4848
///
4949
/// If the value has not yet been read, this method may either return an error or perform a read of the value.
5050
pub async fn value(&self) -> Result<Vec<u8>> {
51-
self.inner.value().map(|val| val.bytes().to_vec()).ok_or_else(|| {
52-
Error::new(
53-
ErrorKind::NotReady,
54-
None,
55-
"the characteristic value has not been read".to_string(),
56-
)
57-
})
51+
self.inner
52+
.value()
53+
.map(|val| val.bytes().to_vec())
54+
.ok_or_else(|| Error::new(ErrorKind::NotReady, None, "the characteristic value has not been read"))
5855
}
5956

6057
/// Read the value of this characteristic from the device
@@ -308,12 +305,6 @@ impl CharacteristicImpl {
308305
.map(|x| Descriptor::new(x, self.delegate.clone()))
309306
.collect()
310307
})
311-
.ok_or_else(|| {
312-
Error::new(
313-
ErrorKind::NotReady,
314-
None,
315-
"no descriptors have been discovered".to_string(),
316-
)
317-
})
308+
.ok_or_else(|| Error::new(ErrorKind::NotReady, None, "no descriptors have been discovered"))
318309
}
319310
}

src/corebluetooth/delegates.rs

+1
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ pub enum PeripheralEvent {
115115
ServicesChanged {
116116
invalidated_services: Vec<ShareId<CBService>>,
117117
},
118+
#[allow(unused)]
118119
L2CAPChannelOpened {
119120
channel: ShareId<CBL2CAPChannel>,
120121
error: Option<ShareId<NSError>>,

src/corebluetooth/descriptor.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,10 @@ impl DescriptorImpl {
6868
///
6969
/// If the value has not yet been read, this method may either return an error or perform a read of the value.
7070
pub async fn value(&self) -> Result<Vec<u8>> {
71-
self.inner.value().map(|val| value_to_slice(&val)).ok_or_else(|| {
72-
Error::new(
73-
ErrorKind::NotReady,
74-
None,
75-
"the descriptor value has not been read".to_string(),
76-
)
77-
})
71+
self.inner
72+
.value()
73+
.map(|val| value_to_slice(&val))
74+
.ok_or_else(|| Error::new(ErrorKind::NotReady, None, "the descriptor value has not been read"))
7875
}
7976

8077
/// Read the value of this descriptor from the device

src/corebluetooth/device.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ impl DeviceImpl {
174174
Error::new(
175175
ErrorKind::NotReady,
176176
None,
177-
"no services have been discovered".to_string(),
177+
"no services have been discovered",
178178
)
179179
})
180180
}

src/corebluetooth/error.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ impl crate::Error {
1010
crate::Error::new(
1111
ErrorKind::Internal,
1212
Some(Box::new(err)),
13-
"receiving delegate event".to_string(),
13+
"receiving delegate event",
1414
)
1515
}
1616

src/corebluetooth/service.rs

+2-14
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,7 @@ impl ServiceImpl {
103103
.map(|x| Characteristic::new(x, self.delegate.clone()))
104104
.collect()
105105
})
106-
.ok_or_else(|| {
107-
Error::new(
108-
ErrorKind::NotReady,
109-
None,
110-
"no characteristics have been discovered".to_string(),
111-
)
112-
})
106+
.ok_or_else(|| Error::new(ErrorKind::NotReady, None, "no characteristics have been discovered"))
113107
}
114108

115109
/// Discover the included services of this service.
@@ -175,12 +169,6 @@ impl ServiceImpl {
175169
self.inner
176170
.included_services()
177171
.map(|s| s.enumerator().map(|x| Service::new(x, self.delegate.clone())).collect())
178-
.ok_or_else(|| {
179-
Error::new(
180-
ErrorKind::NotReady,
181-
None,
182-
"no included services have been discovered".to_string(),
183-
)
184-
})
172+
.ok_or_else(|| Error::new(ErrorKind::NotReady, None, "no included services have been discovered"))
185173
}
186174
}

src/error.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,16 @@ pub struct Error {
99
}
1010

1111
impl Error {
12-
pub(crate) fn new(
12+
pub(crate) fn new<S: ToString>(
1313
kind: ErrorKind,
1414
source: Option<Box<dyn std::error::Error + Send + Sync + 'static>>,
15-
message: String,
15+
message: S,
1616
) -> Self {
17-
Error { kind, source, message }
17+
Error {
18+
kind,
19+
source,
20+
message: message.to_string(),
21+
}
1822
}
1923

2024
/// Returns the corresponding [`ErrorKind`] for this error.

src/windows/adapter.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ impl AdapterImpl {
107107
Error::new(
108108
ErrorKind::Internal,
109109
None,
110-
"adapter event stream closed unexpectedly".to_string(),
110+
"adapter event stream closed unexpectedly",
111111
)
112112
})??;
113113
}

src/windows/characteristic.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ impl CharacteristicImpl {
158158
return Err(Error::new(
159159
ErrorKind::NotSupported,
160160
None,
161-
"characteristic does not support indications or notifications".to_string(),
161+
"characteristic does not support indications or notifications",
162162
));
163163
};
164164

src/windows/error.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ pub(super) fn check_communication_status(
6060
_ => Err(Error::new(
6161
kind_from_communication_status(status, protocol_error)?,
6262
Some(Box::new(CommunicationError(status))),
63-
message.to_string(),
63+
message,
6464
)),
6565
}
6666
}

0 commit comments

Comments
 (0)