sdp: Use .ok() instead of manual match on Ok and Err

Using `.ok()` is more concise when loosing error context in favour of a
simple `None` value.

Automatic replacement SSR pattern in rust-analyzer:

    {let $a = $b;match $c {Ok($d) => Some($e), Err(_) => None }} ==>> {$b.ok()}

Note that rust-analyzer does not support:
- duplicate labels (ie. $c should be equal to $a, and $e equal to $d);
- statement lists yet, hence both sides are wrapped in braces.

But it performs the desired operation well enough here.
This commit is contained in:
Marijn Suijten 2021-01-07 22:49:11 +01:00
parent 15505cc5b8
commit f4ad451956
2 changed files with 14 additions and 70 deletions

View file

@ -228,11 +228,7 @@ impl SDPMediaRef {
if ptr.is_null() { if ptr.is_null() {
None None
} else { } else {
let result = CStr::from_ptr(ptr).to_str(); CStr::from_ptr(ptr).to_str().ok()
match result {
Ok(attr) => Some(attr),
Err(_) => None,
}
} }
} }
} }
@ -243,11 +239,7 @@ impl SDPMediaRef {
if ptr.is_null() { if ptr.is_null() {
None None
} else { } else {
let result = CStr::from_ptr(ptr).to_str(); CStr::from_ptr(ptr).to_str().ok()
match result {
Ok(attr) => Some(attr),
Err(_) => None,
}
} }
} }
} }
@ -296,11 +288,7 @@ impl SDPMediaRef {
if ptr.is_null() { if ptr.is_null() {
None None
} else { } else {
let result = CStr::from_ptr(ptr).to_str(); CStr::from_ptr(ptr).to_str().ok()
match result {
Ok(attr) => Some(attr),
Err(_) => None,
}
} }
} }
} }
@ -311,11 +299,7 @@ impl SDPMediaRef {
if ptr.is_null() { if ptr.is_null() {
None None
} else { } else {
let result = CStr::from_ptr(ptr).to_str(); CStr::from_ptr(ptr).to_str().ok()
match result {
Ok(attr) => Some(attr),
Err(_) => None,
}
} }
} }
} }
@ -337,11 +321,7 @@ impl SDPMediaRef {
if ptr.is_null() { if ptr.is_null() {
None None
} else { } else {
let result = CStr::from_ptr(ptr).to_str(); CStr::from_ptr(ptr).to_str().ok()
match result {
Ok(attr) => Some(attr),
Err(_) => None,
}
} }
} }
} }
@ -360,11 +340,7 @@ impl SDPMediaRef {
if ptr.is_null() { if ptr.is_null() {
None None
} else { } else {
let result = CStr::from_ptr(ptr).to_str(); CStr::from_ptr(ptr).to_str().ok()
match result {
Ok(attr) => Some(attr),
Err(_) => None,
}
} }
} }
} }

View file

@ -266,11 +266,7 @@ impl SDPMessageRef {
if ptr.is_null() { if ptr.is_null() {
None None
} else { } else {
let result = CStr::from_ptr(ptr).to_str(); CStr::from_ptr(ptr).to_str().ok()
match result {
Ok(attr) => Some(attr),
Err(_) => None,
}
} }
} }
} }
@ -281,11 +277,7 @@ impl SDPMessageRef {
if ptr.is_null() { if ptr.is_null() {
None None
} else { } else {
let result = CStr::from_ptr(ptr).to_str(); CStr::from_ptr(ptr).to_str().ok()
match result {
Ok(attr) => Some(attr),
Err(_) => None,
}
} }
} }
} }
@ -326,11 +318,7 @@ impl SDPMessageRef {
if ptr.is_null() { if ptr.is_null() {
None None
} else { } else {
let result = CStr::from_ptr(ptr).to_str(); CStr::from_ptr(ptr).to_str().ok()
match result {
Ok(attr) => Some(attr),
Err(_) => None,
}
} }
} }
} }
@ -341,11 +329,7 @@ impl SDPMessageRef {
if ptr.is_null() { if ptr.is_null() {
None None
} else { } else {
let result = CStr::from_ptr(ptr).to_str(); CStr::from_ptr(ptr).to_str().ok()
match result {
Ok(attr) => Some(attr),
Err(_) => None,
}
} }
} }
} }
@ -412,11 +396,7 @@ impl SDPMessageRef {
if ptr.is_null() { if ptr.is_null() {
None None
} else { } else {
let result = CStr::from_ptr(ptr).to_str(); CStr::from_ptr(ptr).to_str().ok()
match result {
Ok(attr) => Some(attr),
Err(_) => None,
}
} }
} }
} }
@ -427,11 +407,7 @@ impl SDPMessageRef {
if ptr.is_null() { if ptr.is_null() {
None None
} else { } else {
let result = CStr::from_ptr(ptr).to_str(); CStr::from_ptr(ptr).to_str().ok()
match result {
Ok(attr) => Some(attr),
Err(_) => None,
}
} }
} }
} }
@ -457,11 +433,7 @@ impl SDPMessageRef {
if ptr.is_null() { if ptr.is_null() {
None None
} else { } else {
let result = CStr::from_ptr(ptr).to_str(); CStr::from_ptr(ptr).to_str().ok()
match result {
Ok(attr) => Some(attr),
Err(_) => None,
}
} }
} }
} }
@ -472,11 +444,7 @@ impl SDPMessageRef {
if ptr.is_null() { if ptr.is_null() {
None None
} else { } else {
let result = CStr::from_ptr(ptr).to_str(); CStr::from_ptr(ptr).to_str().ok()
match result {
Ok(attr) => Some(attr),
Err(_) => None,
}
} }
} }
} }