@@ -8,7 +8,7 @@ use crate::Result;
88#[ cfg( feature = "fs" ) ]
99use crate :: { fcntl:: OFlag , sys:: stat:: Mode } ;
1010use libc:: { self , c_int, c_void, off_t, size_t} ;
11- use std:: { num:: NonZeroUsize , os:: unix:: io:: RawFd } ;
11+ use std:: { num:: NonZeroUsize , os:: unix:: io:: { AsRawFd , AsFd } } ;
1212
1313libc_bitflags ! {
1414 /// Desired memory protection of a memory mapping.
@@ -416,17 +416,18 @@ pub fn munlockall() -> Result<()> {
416416/// See the [`mmap(2)`] man page for detailed requirements.
417417///
418418/// [`mmap(2)`]: https://man7.org/linux/man-pages/man2/mmap.2.html
419- pub unsafe fn mmap (
419+ pub unsafe fn mmap < F : AsFd > (
420420 addr : Option < NonZeroUsize > ,
421421 length : NonZeroUsize ,
422422 prot : ProtFlags ,
423423 flags : MapFlags ,
424- fd : RawFd ,
424+ f : Option < & F > ,
425425 offset : off_t ,
426426) -> Result < * mut c_void > {
427427 let ptr =
428428 addr. map_or ( std:: ptr:: null_mut ( ) , |a| usize:: from ( a) as * mut c_void ) ;
429429
430+ let fd = f. map ( |f| f. as_fd ( ) . as_raw_fd ( ) ) . unwrap_or ( -1 ) ;
430431 let ret =
431432 libc:: mmap ( ptr, length. into ( ) , prot. bits ( ) , flags. bits ( ) , fd, offset) ;
432433
@@ -566,9 +567,11 @@ pub fn shm_open<P>(
566567 name: & P ,
567568 flag: OFlag ,
568569 mode: Mode
569- ) -> Result <RawFd >
570+ ) -> Result <std :: os :: unix :: io :: OwnedFd >
570571 where P : ?Sized + NixPath
571572{
573+ use std:: os:: unix:: io:: { FromRawFd , OwnedFd } ;
574+
572575 let ret = name. with_nix_path( |cstr| {
573576 #[ cfg( any( target_os = "macos" , target_os = "ios" ) ) ]
574577 unsafe {
@@ -580,7 +583,10 @@ pub fn shm_open<P>(
580583 }
581584 } ) ?;
582585
583- Errno :: result( ret)
586+ match ret {
587+ -1 => Err ( Errno :: last( ) ) ,
588+ fd => Ok ( unsafe { OwnedFd :: from_raw_fd( fd) } )
589+ }
584590}
585591}
586592
0 commit comments