@@ -11,11 +11,7 @@ use crate::zxio::{
11
11
use bitflags:: bitflags;
12
12
use fidl:: { encoding:: const_assert_eq, endpoints:: ServerEnd } ;
13
13
use fidl_fuchsia_io as fio;
14
- use fuchsia_zircon:: {
15
- self as zx,
16
- sys:: { ZX_ERR_INVALID_ARGS , ZX_ERR_NO_MEMORY , ZX_OK } ,
17
- zx_status_t, HandleBased ,
18
- } ;
14
+ use fuchsia_zircon:: { self as zx, AsHandleRef as _, HandleBased as _} ;
19
15
use std:: {
20
16
ffi:: CStr ,
21
17
mem:: { size_of, size_of_val} ,
@@ -24,7 +20,7 @@ use std::{
24
20
} ;
25
21
use zerocopy:: { AsBytes , FromBytes } ;
26
22
use zxio:: {
27
- msghdr, sockaddr, sockaddr_storage, socklen_t, zx_handle_t, zxio_object_type_t,
23
+ msghdr, sockaddr, sockaddr_storage, socklen_t, zx_handle_t, zx_status_t , zxio_object_type_t,
28
24
zxio_seek_origin_t, zxio_storage_t, ZXIO_SHUTDOWN_OPTIONS_READ , ZXIO_SHUTDOWN_OPTIONS_WRITE ,
29
25
} ;
30
26
@@ -411,31 +407,30 @@ impl std::fmt::Debug for Zxio {
411
407
// in a more general function pointer (`fn(&str, zx::Channel) -> zx::Status`)
412
408
// rather than having to implement this trait.
413
409
pub trait ServiceConnector {
414
- /// Connect `server_end` to the protocol available at `service `.
415
- fn connect ( service : & str , server_end : zx:: Channel ) -> zx:: Status ;
410
+ /// Returns a channel to the service named by `service_name `.
411
+ fn connect ( service_name : & str ) -> Result < & ' static zx:: Channel , zx:: Status > ;
416
412
}
417
413
418
- /// Connect a handle to a specified service.
414
+ /// Sets `provider_handle` to a handle to the service named by `service_name`.
415
+ ///
416
+ /// This function is intended to be passed to zxio_socket().
419
417
///
420
418
/// SAFETY: Dereferences the raw pointers `service_name` and `provider_handle`.
421
419
unsafe extern "C" fn service_connector < S : ServiceConnector > (
422
420
service_name : * const c_char ,
423
421
provider_handle : * mut zx_handle_t ,
424
422
) -> zx_status_t {
425
- let ( client_end, server_end) = zx:: Channel :: create ( ) ;
426
-
427
- let service = match CStr :: from_ptr ( service_name) . to_str ( ) {
428
- Ok ( s) => s,
429
- Err ( _) => return ZX_ERR_INVALID_ARGS ,
430
- } ;
431
-
432
- let status = S :: connect ( service, server_end) . into_raw ( ) ;
433
- if status != ZX_OK {
434
- return status;
435
- }
436
-
437
- * provider_handle = client_end. into_handle ( ) . into_raw ( ) ;
438
- ZX_OK
423
+ let status: zx:: Status = ( || {
424
+ let service_name = CStr :: from_ptr ( service_name)
425
+ . to_str ( )
426
+ . map_err ( |std:: str:: Utf8Error { .. } | zx:: Status :: INVALID_ARGS ) ?;
427
+
428
+ S :: connect ( service_name) . map ( |channel| {
429
+ * provider_handle = channel. raw_handle ( ) ;
430
+ } )
431
+ } ) ( )
432
+ . into ( ) ;
433
+ status. into_raw ( )
439
434
}
440
435
441
436
/// Sets `out_storage` as the zxio_storage of `out_context`.
@@ -449,13 +444,17 @@ unsafe extern "C" fn storage_allocator(
449
444
out_context : * mut * mut c_void ,
450
445
) -> zx_status_t {
451
446
let zxio_ptr_ptr = out_context as * mut * mut zxio_storage_t ;
452
- if let Some ( zxio_ptr) = zxio_ptr_ptr. as_mut ( ) {
453
- if let Some ( zxio) = zxio_ptr. as_mut ( ) {
454
- * out_storage = zxio;
455
- return ZX_OK ;
447
+ let status: zx:: Status = ( || {
448
+ if let Some ( zxio_ptr) = zxio_ptr_ptr. as_mut ( ) {
449
+ if let Some ( zxio) = zxio_ptr. as_mut ( ) {
450
+ * out_storage = zxio;
451
+ return Ok ( ( ) ) ;
452
+ }
456
453
}
457
- }
458
- ZX_ERR_NO_MEMORY
454
+ Err ( zx:: Status :: NO_MEMORY )
455
+ } ) ( )
456
+ . into ( ) ;
457
+ status. into_raw ( )
459
458
}
460
459
461
460
impl Zxio {
@@ -1305,11 +1304,10 @@ mod test {
1305
1304
use super :: * ;
1306
1305
1307
1306
use anyhow:: Error ;
1308
- use fidl:: endpoints:: Proxy ;
1307
+ use fidl:: endpoints:: Proxy as _ ;
1309
1308
use fidl_fuchsia_io as fio;
1310
1309
use fuchsia_async as fasync;
1311
1310
use fuchsia_fs:: directory;
1312
- use fuchsia_zircon:: { AsHandleRef , HandleBased } ;
1313
1311
1314
1312
fn open_pkg ( ) -> fio:: DirectorySynchronousProxy {
1315
1313
let pkg_proxy = directory:: open_in_namespace (
@@ -1470,7 +1468,7 @@ mod test {
1470
1468
& mut out_context_ptr as * mut * mut Zxio as * mut * mut c_void ,
1471
1469
)
1472
1470
} ;
1473
- assert_eq ! ( out, ZX_OK ) ;
1471
+ assert_eq ! ( out, zx :: sys :: ZX_OK ) ;
1474
1472
}
1475
1473
1476
1474
#[ fuchsia:: test]
@@ -1487,6 +1485,6 @@ mod test {
1487
1485
out_context,
1488
1486
)
1489
1487
} ;
1490
- assert_eq ! ( out, ZX_ERR_NO_MEMORY ) ;
1488
+ assert_eq ! ( out, zx :: sys :: ZX_ERR_NO_MEMORY ) ;
1491
1489
}
1492
1490
}
0 commit comments