1
1
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
2
2
3
+ use core:: str;
3
4
use std:: borrow:: Cow ;
4
5
use std:: path:: Path ;
5
6
use std:: path:: PathBuf ;
@@ -288,7 +289,7 @@ pub trait FileSystem: std::fmt::Debug + MaybeSend + MaybeSync {
288
289
& self ,
289
290
path : & Path ,
290
291
access_check : Option < AccessCheckCb > ,
291
- ) -> FsResult < Vec < u8 > > {
292
+ ) -> FsResult < Cow < ' static , [ u8 ] > > {
292
293
let options = OpenOptions :: read ( ) ;
293
294
let file = self . open_sync ( path, options, access_check) ?;
294
295
let buf = file. read_all_sync ( ) ?;
@@ -298,7 +299,7 @@ pub trait FileSystem: std::fmt::Debug + MaybeSend + MaybeSync {
298
299
& ' a self ,
299
300
path : PathBuf ,
300
301
access_check : Option < AccessCheckCb < ' a > > ,
301
- ) -> FsResult < Vec < u8 > > {
302
+ ) -> FsResult < Cow < ' static , [ u8 ] > > {
302
303
let options = OpenOptions :: read ( ) ;
303
304
let file = self . open_async ( path, options, access_check) . await ?;
304
305
let buf = file. read_all_async ( ) . await ?;
@@ -327,17 +328,25 @@ pub trait FileSystem: std::fmt::Debug + MaybeSend + MaybeSync {
327
328
& self ,
328
329
path : & Path ,
329
330
access_check : Option < AccessCheckCb > ,
330
- ) -> FsResult < String > {
331
+ ) -> FsResult < Cow < ' static , str > > {
331
332
let buf = self . read_file_sync ( path, access_check) ?;
332
- Ok ( string_from_utf8_lossy ( buf) )
333
+ Ok ( string_from_cow_utf8_lossy ( buf) )
333
334
}
334
335
async fn read_text_file_lossy_async < ' a > (
335
336
& ' a self ,
336
337
path : PathBuf ,
337
338
access_check : Option < AccessCheckCb < ' a > > ,
338
- ) -> FsResult < String > {
339
+ ) -> FsResult < Cow < ' static , str > > {
339
340
let buf = self . read_file_async ( path, access_check) . await ?;
340
- Ok ( string_from_utf8_lossy ( buf) )
341
+ Ok ( string_from_cow_utf8_lossy ( buf) )
342
+ }
343
+ }
344
+
345
+ #[ inline( always) ]
346
+ fn string_from_cow_utf8_lossy ( buf : Cow < ' static , [ u8 ] > ) -> Cow < ' static , str > {
347
+ match buf {
348
+ Cow :: Owned ( buf) => Cow :: Owned ( string_from_utf8_lossy ( buf) ) ,
349
+ Cow :: Borrowed ( buf) => String :: from_utf8_lossy ( buf) ,
341
350
}
342
351
}
343
352
0 commit comments