1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
//! This crate contains macros for statically including resources in release mode,
//! but dynamically loading them in debug mode.
//!
//! This is primarily intended for games, allowing you to both avoid file IO in
//! release builds and dynamically reload resources in debug mode.
//!
//! You can change the default behaviour, in debug or release mode, by using the
//! `force-static` and `force-dynamic` features.
//!
//! ```rust,ignore
//! use resource::{resource, resource_str};
//!
//! // Include text
//! let readme_text = resource_str!("README.md");
//!
//! // Include bytes
//! let logo_bytes = resource!("assets/logo.png");
//!
//! // Load multiple strings
//! let translations = resource_str!(["english.txt", "italiano.txt"]);
//!
//! // Load and process multiple binary resources
//! let (light_texture, dark_texture) = resource!(
//!     ("assets/light.png", "assets/dark.png"),
//!     Texture::decode);
//! ```

#[cfg(all(feature = "force-static", feature = "force-dynamic"))]
compile_error!("resource: Cannot enable both the force-static and force-dynamic features.");

pub use self::resource::Resource;

use std::path::Path;

/// Used internally.
///
/// Only used by the dynamic versions of `Resource` to make it generic
/// over both strings and bytes. Represents something that can be read
/// straight from a file.
pub trait ReadFromFile {
    fn read_from_file(path: &Path) -> Self;
}

impl ReadFromFile for String {
    fn read_from_file(path: &Path) -> String {
        std::fs::read_to_string(path)
            .map_err(|e| eprintln!("Failed to read `{}` as string: {}", path.display(), e))
            .unwrap()
    }
}

impl ReadFromFile for Vec<u8> {
    fn read_from_file(path: &Path) -> Vec<u8> {
        std::fs::read(path)
            .map_err(|e| eprintln!("Failed to read `{}` as bytes: {}", path.display(), e))
            .unwrap()
    }
}

#[cfg(any(
    feature = "force-dynamic",
    all(not(feature = "force-static"), debug_assertions)
))]
mod resource {
    use std::{
        borrow::{Cow, ToOwned},
        convert::AsRef,
        ops::Deref,
        path::{Path, PathBuf},
        time::SystemTime,
    };

    use crate::ReadFromFile;

    /// A resource (string or binary) loaded in memory.
    ///
    /// In debug mode, this structure contains the data, the path to the file,
    /// and a timestamp in order to support the `reload_if_changed` method.
    ///
    /// In release mode, it contains only an immutable, static reference to
    /// the data.
    ///
    /// This struct implements `Deref` and `AsRef` (for the `&str`
    /// and `&[u8]` types respectively) which allows you to refer
    /// transparently to the data.
    ///
    /// Alternatively, it also implements `Into<Cow<'static, T>>`. In debug mode,
    /// this will return a `Cow` that owns the data. In release mode, it returns
    /// a `Cow` that borrows the static data.
    pub struct Resource<B>(<B as ToOwned>::Owned, PathBuf, SystemTime)
    where
        B: 'static + ToOwned + ?Sized;

    impl<B> Resource<B>
    where
        B: 'static + ToOwned + ?Sized,
        B::Owned: ReadFromFile,
    {
        #[doc(hidden)]
        /// Please don't call this directly. It has to be public for the macro
        /// but you shouldn't call it because it's not stable.
        pub fn _from_file(path: &str) -> Self {
            let path = PathBuf::from(path);
            let data = B::Owned::read_from_file(&path);
            let modified = Self::modified(&path).unwrap_or(SystemTime::UNIX_EPOCH);

            Resource(data, path, modified)
        }

        fn modified(path: &Path) -> Option<SystemTime> {
            std::fs::metadata(&path)
                .and_then(|metadata| metadata.modified())
                .ok()
        }

        /// Returns `true` if the resource has changed since loading.
        ///
        /// In release mode, always returns `false`.
        pub fn changed(&self) -> bool {
            let modified = Self::modified(&self.1);
            modified.is_some() && modified != Some(self.2)
        }

        /// Reloads the resource.
        ///
        /// In release mode, does nothing.
        pub fn reload(&mut self) {
            let data = B::Owned::read_from_file(&self.1);
            let modified = Self::modified(&self.1).unwrap_or(SystemTime::UNIX_EPOCH);
            self.0 = data;
            self.2 = modified;
        }

        /// Reloads the resource only if it has changed since the previous
        /// load. Returns `true` if the resource was reloaded.
        ///
        /// In release mode, does nothing.
        pub fn reload_if_changed(&mut self) -> bool {
            let changed = self.changed();
            if changed {
                self.reload();
            }
            changed
        }
    }

    impl<B> AsRef<B> for Resource<B>
    where
        B: 'static + ToOwned + ?Sized,
        B::Owned: AsRef<B>,
    {
        fn as_ref(&self) -> &B {
            self.0.as_ref()
        }
    }

    impl<B> Deref for Resource<B>
    where
        B: 'static + ToOwned + ?Sized,
        B::Owned: AsRef<B>,
    {
        type Target = B;

        fn deref(&self) -> &Self::Target {
            self.as_ref()
        }
    }

    impl<B> Into<Cow<'static, B>> for Resource<B>
    where
        B: 'static + ToOwned + ?Sized,
    {
        fn into(self) -> Cow<'static, B> {
            Cow::Owned(self.0)
        }
    }

    impl<B> Clone for Resource<B>
    where
        B: 'static + ToOwned + ?Sized,
        B::Owned: Clone,
    {
        fn clone(&self) -> Self {
            Resource(self.0.clone(), self.1.clone(), self.2)
        }
    }
}

#[cfg(any(
    feature = "force-static",
    all(not(feature = "force-dynamic"), not(debug_assertions))
))]
mod resource {
    use std::{
        borrow::{Cow, ToOwned},
        convert::AsRef,
        ops::Deref,
    };

    use crate::ReadFromFile;

    pub struct Resource<B>(&'static B)
    where
        B: 'static + ToOwned + ?Sized;

    impl<B> Resource<B>
    where
        B: 'static + ToOwned + ?Sized,
        B::Owned: ReadFromFile,
    {
        #[doc(hidden)]
        /// Please don't call this directly. It has to be public for the macro
        /// but you shouldn't call it because it's not stable.
        pub fn _from_data(data: &'static B) -> Self {
            Resource(data)
        }

        pub fn changed(&self) -> bool {
            false
        }

        pub fn reload_if_changed(&mut self) -> bool {
            false
        }

        pub fn reload(&mut self) {}
    }

    impl<B> AsRef<B> for Resource<B>
    where
        B: 'static + ToOwned + ?Sized,
        B::Owned: AsRef<B>,
    {
        fn as_ref(&self) -> &B {
            self.0
        }
    }

    impl<B> Deref for Resource<B>
    where
        B: 'static + ToOwned + ?Sized,
        B::Owned: AsRef<B>,
    {
        type Target = B;

        fn deref(&self) -> &Self::Target {
            self.0
        }
    }

    impl<B> Into<Cow<'static, B>> for Resource<B>
    where
        B: 'static + ToOwned + ?Sized,
    {
        fn into(self) -> Cow<'static, B> {
            Cow::Borrowed(self.0)
        }
    }

    impl<B> Clone for Resource<B>
    where
        B: 'static + ToOwned + ?Sized,
        B::Owned: Clone,
    {
        fn clone(&self) -> Self {
            Resource(self.0)
        }
    }
}

/// Load text resources statically in release mode, or dynamically in debug.
///
/// The filenames are relative to the root of your crate.
///
/// If you wish to override the static or dynamic behaviour, you can use the
/// `force-static` or `force-dynamic` features.
///
/// This macro optionally takes a function which can be used to transform the
/// contents of each file on load.
///
/// # Panics
///
/// When dynamically including, this will panic if any file does not exist. When
/// statically including, this will be a compile error and will never panic.
///
/// # Examples
///
/// Load a single text file:
///
/// ```rust
/// use resource::resource_str;
///
/// let toml = resource_str!("Cargo.toml");
/// assert!(toml.contains("[package]"));
/// ```
///
/// Load an array or tuple of text files:
///
/// ```rust
/// use resource::resource_str;
///
/// let (toml, lib) = resource_str!(("Cargo.toml", "src/lib.rs"));
/// let as_array = resource_str!(["Cargo.toml", "src/lib.rs"]);
///
/// assert_eq!(toml.as_ref(), as_array[0].as_ref());
/// assert_eq!(lib.as_ref(), as_array[1].as_ref());
/// ```
///
/// Load multiple text files and apply a transformation to each one:
///
/// ```rust
/// use resource::resource_str;
///
/// let [toml, lib] = resource_str!(["Cargo.toml", "src/lib.rs"], str::to_uppercase);
///
/// assert!(toml.contains("RESOURCE"));
/// assert!(lib.contains("MACRO_RULES"));
/// ```
#[cfg(any(
    feature = "force-dynamic",
    all(not(feature = "force-static"), debug_assertions)
))]
#[macro_export]
macro_rules! resource_str {
    ([ $($filenames:tt),* $(,)* ], $load_fn:expr) => {
        [ $(resource_str!($filenames, $load_fn)),* ]
    };

    (( $($filenames:tt),* $(,)* ), $load_fn:expr) => {
        ( $(resource_str!($filenames, $load_fn)),* )
    };

    ([ $($filenames:tt),* $(,)* ]) => {
        [ $(resource_str!($filenames)),* ]
    };

    (( $($filenames:tt),* $(,)* )) => {
        ( $(resource_str!($filenames)),* )
    };

    ($filename:tt, $load_fn:expr) => {
        $load_fn(
            <$crate::Resource<str> as std::convert::AsRef<str>>::as_ref(&resource_str!($filename))
        )
    };

    ($filename:tt) => {
        $crate::Resource::<str>::_from_file(concat!(env!("CARGO_MANIFEST_DIR"), "/", $filename))
    };
}

#[cfg(any(
    feature = "force-static",
    all(not(feature = "force-dynamic"), not(debug_assertions))
))]
#[macro_export]
macro_rules! resource_str {
    ([ $($filenames:tt),* $(,)* ], $load_fn:expr) => {
        [ $(resource_str!($filenames, $load_fn)),* ]
    };

    (( $($filenames:tt),* $(,)* ), $load_fn:expr) => {
        ( $(resource_str!($filenames, $load_fn)),* )
    };

    ([ $($filenames:tt),* $(,)* ]) => {
        [ $(resource_str!($filenames)),* ]
    };

    (( $($filenames:tt),* $(,)* )) => {
        ( $(resource_str!($filenames)),* )
    };

    ($filename:tt, $load_fn:expr) => {
        $load_fn(
            <$crate::Resource<str> as std::convert::AsRef<str>>::as_ref(&resource_str!($filename))
        )
    };

    ($filename:tt) => {
        $crate::Resource::<str>::_from_data(include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/", $filename)))
    };
}

/// Load binary resources statically in release mode, or dynamically in
/// debug.
///
/// The filenames are relative to the root of your crate.
///
/// If you wish to override the static or dynamic behaviour, you can use the
/// `force-static` or `force-dynamic` features.
///
/// This macro optionally takes a function which can be used to transform the
/// contents of each file on load.
///
/// # Panics
///
/// When dynamically including, this will panic if any file does not exist. When
/// statically including, this will be a compile error and will never panic.
///
/// # Examples
///
/// Load a single binary file:
///
/// ```rust
/// use resource::resource;
///
/// let toml = resource!("Cargo.toml");
/// assert_eq!(&toml[0..9], b"[package]");
/// ```
///
/// Load an array or tuple of binary files:
///
/// ```rust
/// use resource::resource;
///
/// let (toml, lib) = resource!(("Cargo.toml", "src/lib.rs"));
/// let as_array = resource!(["Cargo.toml", "src/lib.rs"]);
///
/// assert_eq!(toml.as_ref(), as_array[0].as_ref());
/// assert_eq!(lib.as_ref(), as_array[1].as_ref());
/// ```
///
/// Load binary files and apply a transformation to each one:
///
/// ```rust
/// use resource::resource;
///
/// let [toml, lib] = resource!(["Cargo.toml", "src/lib.rs"],
///     |bytes: &[u8]| bytes.to_ascii_uppercase());
///
/// assert_eq!(&toml[0..9], b"[PACKAGE]");
/// assert_eq!(&lib[0..4], b"//! ");
/// ```
#[cfg(any(
    feature = "force-dynamic",
    all(not(feature = "force-static"), debug_assertions)
))]
#[macro_export]
macro_rules! resource {
    ([ $($filenames:tt),* $(,)* ], $load_fn:expr) => {
        [ $(resource!($filenames, $load_fn)),* ]
    };

    (( $($filenames:tt),* $(,)* ), $load_fn:expr) => {
        ( $(resource!($filenames, $load_fn)),* )
    };

    ([ $($filenames:tt),* $(,)* ]) => {
        [ $(resource!($filenames)),* ]
    };

    (( $($filenames:tt),* $(,)* )) => {
        ( $(resource!($filenames)),* )
    };

    ($filename:tt, $load_fn:expr) => {
        $load_fn(
            <$crate::Resource<[u8]> as std::convert::AsRef<[u8]>>::as_ref(&resource!($filename))
        )
    };

    ($filename:tt) => {
        $crate::Resource::<[u8]>::_from_file(concat!(env!("CARGO_MANIFEST_DIR"), "/", $filename))
    };
}

#[cfg(any(
    feature = "force-static",
    all(not(feature = "force-dynamic"), not(debug_assertions))
))]
#[macro_export]
macro_rules! resource {
    ([ $($filenames:tt),* $(,)* ], $load_fn:expr) => {
        [ $(resource!($filenames, $load_fn)),* ]
    };

    (( $($filenames:tt),* $(,)* ), $load_fn:expr) => {
        ( $(resource!($filenames, $load_fn)),* )
    };

    ([ $($filenames:tt),* $(,)* ]) => {
        [ $(resource!($filenames)),* ]
    };

    (( $($filenames:tt),* $(,)* )) => {
        ( $(resource!($filenames)),* )
    };

    ($filename:tt, $load_fn:expr) => {
        $load_fn(
            <$crate::Resource<[u8]> as std::convert::AsRef<[u8]>>::as_ref(&resource!($filename))
        )
    };

    ($filename:tt) => {
        $crate::Resource::<[u8]>::_from_data(include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/", $filename)))
    };
}

#[cfg(test)]
mod single_file_transform_tests {
    fn rev_string(string: &str) -> String {
        string.chars().rev().collect()
    }

    fn rev_bytes(bytes: &[u8]) -> Vec<u8> {
        bytes.iter().cloned().rev().collect()
    }

    #[test]
    fn transform_single_string() {
        let s = resource_str!("tests/str.txt", rev_string);
        assert_eq!(s, "\ngnirts\na\nsi\nsihT");
    }

    #[test]
    fn transform_single_bytes() {
        let s = resource!("tests/bytes.bin", rev_bytes);
        assert_eq!(s, &[52, 51, 50, 49, 48]);
    }
}

#[cfg(test)]
mod multi_file_tests {
    fn rev_string(string: &str) -> String {
        string.chars().rev().collect()
    }

    fn rev_bytes(bytes: &[u8]) -> Vec<u8> {
        bytes.iter().cloned().rev().collect()
    }

    #[test]
    fn load_array_of_multiple_strings() {
        let [a, b, c] = resource_str!([
            "tests/string_a.txt",
            "tests/string_b.txt",
            "tests/string_c.txt",
        ]);

        assert_eq!([&*a, &*b, &*c], ["String A\n", "String B\n", "String C\n"]);
    }

    #[test]
    fn load_tuple_of_multiple_strings() {
        let (a, b, c) = resource_str!((
            "tests/string_a.txt",
            "tests/string_b.txt",
            "tests/string_c.txt",
        ));

        assert_eq!(&*a, "String A\n");
        assert_eq!(&*b, "String B\n");
        assert_eq!(&*c, "String C\n");
    }

    #[test]
    fn load_array_of_multiple_bytes() {
        let [a, b, c] = resource!([
            "tests/bytes_a.bin",
            "tests/bytes_b.bin",
            "tests/bytes_c.bin",
        ]);

        assert_eq!(
            [&*a, &*b, &*c],
            [
                b"Bytes A".as_ref(),
                b"Bytes B".as_ref(),
                b"Bytes C".as_ref()
            ]
        );
    }

    #[test]
    fn load_tuple_of_multiple_bytes() {
        let (a, b, c) = resource!((
            "tests/bytes_a.bin",
            "tests/bytes_b.bin",
            "tests/bytes_c.bin",
        ));

        assert_eq!(&*a, b"Bytes A".as_ref());
        assert_eq!(&*b, b"Bytes B".as_ref());
        assert_eq!(&*c, b"Bytes C".as_ref());
    }

    #[test]
    fn load_with_fn_array_of_multiple_strings() {
        let [a, b, c] = resource_str!(
            [
                "tests/string_a.txt",
                "tests/string_b.txt",
                "tests/string_c.txt",
            ],
            rev_string
        );

        assert_eq!([a, b, c], ["\nA gnirtS", "\nB gnirtS", "\nC gnirtS"]);
    }

    #[test]
    fn load_with_fn_tuple_of_multiple_strings() {
        let (a, b, c) = resource_str!(
            (
                "tests/string_a.txt",
                "tests/string_b.txt",
                "tests/string_c.txt",
            ),
            rev_string
        );

        assert_eq!(a, "\nA gnirtS");
        assert_eq!(b, "\nB gnirtS");
        assert_eq!(c, "\nC gnirtS");
    }

    #[test]
    fn load_with_fn_array_of_multiple_bytes() {
        let [a, b, c] = resource!(
            [
                "tests/bytes_a.bin",
                "tests/bytes_b.bin",
                "tests/bytes_c.bin",
            ],
            rev_bytes
        );

        assert_eq!(
            [a, b, c],
            [
                b"A setyB".as_ref(),
                b"B setyB".as_ref(),
                b"C setyB".as_ref()
            ]
        );
    }

    #[test]
    fn load_with_fn_tuple_of_multiple_bytes() {
        let (a, b, c) = resource!(
            (
                "tests/bytes_a.bin",
                "tests/bytes_b.bin",
                "tests/bytes_c.bin",
            ),
            rev_bytes
        );

        assert_eq!(a, b"A setyB".as_ref());
        assert_eq!(b, b"B setyB".as_ref());
        assert_eq!(c, b"C setyB".as_ref());
    }
}

#[cfg(test)]
#[cfg(any(
    feature = "force-dynamic",
    all(not(feature = "force-static"), debug_assertions)
))]
mod dynamic_tests {
    use super::*;

    use std::borrow::Cow;

    #[test]
    fn str_dynamic() {
        match resource_str!("tests/str.txt").into() {
            Cow::Owned(ref s) if s == "This\nis\na\nstring\n" => (),
            _ => panic!("Expected owned string!"),
        }
    }

    #[test]
    fn bytes_dynamic() {
        match resource!("tests/bytes.bin").into() {
            Cow::Owned(ref s) if s == &[48, 49, 50, 51, 52] => (),
            _ => panic!("Expected owned bytes!"),
        }
    }
}

#[cfg(test)]
#[cfg(any(
    feature = "force-static",
    all(not(feature = "force-dynamic"), not(debug_assertions))
))]
mod static_tests {
    use super::*;

    use std::borrow::Cow;

    #[test]
    fn str_static() {
        match resource_str!("tests/str.txt").into() {
            Cow::Borrowed(s) if s == "This\nis\na\nstring\n" => (),
            _ => panic!("Expected borrowed string!"),
        }
    }

    #[test]
    fn bytes_static() {
        match resource!("tests/bytes.bin").into() {
            Cow::Borrowed(s) if s == &[48, 49, 50, 51, 52] => (),
            _ => panic!("Expected borrowed bytes!"),
        }
    }
}

#[cfg(test)]
#[cfg(any(
    feature = "force-static",
    all(not(feature = "force-dynamic"), not(debug_assertions))
))]
mod static_reload_tests {

    #[test]
    fn changed() {
        std::fs::write("tests/temp/static_changed.txt", "Old").unwrap();

        let res = resource_str!("tests/temp/static_changed.txt");
        assert!(!res.changed());

        std::fs::write("tests/temp/static_changed.txt", "New").unwrap();
        let changed = res.changed();

        // Revert
        std::fs::write("tests/temp/static_changed.txt", "Old").unwrap();

        assert!(!changed);
    }

    #[test]
    fn reload() {
        std::fs::write("tests/temp/static_reload.txt", "Old").unwrap();

        let mut res = resource_str!("tests/temp/static_reload.txt");
        assert_eq!(res.as_ref(), "Old");

        std::fs::write("tests/temp/static_reload.txt", "New").unwrap();
        res.reload();

        // Revert
        std::fs::write("tests/temp/static_reload.txt", "Old").unwrap();

        assert_eq!(res.as_ref(), "Old");
    }

    #[test]
    fn reload_if_changed() {
        std::fs::write("tests/temp/static_reload_if_changed.txt", "Old").unwrap();

        let mut res = resource_str!("tests/temp/static_reload_if_changed.txt");
        assert!(!res.reload_if_changed());
        assert_eq!(res.as_ref(), "Old");

        std::fs::write("tests/temp/static_reload_if_changed.txt", "New").unwrap();
        let changed = res.reload_if_changed();

        // Revert
        std::fs::write("tests/temp/static_reload_if_changed.txt", "Old").unwrap();

        assert!(!changed);
        assert_eq!(res.as_ref(), "Old");
    }
}

#[cfg(test)]
#[cfg(any(
    feature = "force-dynamic",
    all(not(feature = "force-static"), debug_assertions)
))]
mod dynamic_reload_tests {

    #[test]
    fn changed() {
        // For some reason, `changed()` doesn't seem to work on Travis.
        if option_env!("TRAVIS").is_none() {
            std::fs::write("tests/temp/dynamic_changed.txt", "Old").unwrap();

            let res = resource_str!("tests/temp/dynamic_changed.txt");
            assert!(!res.changed());

            std::fs::write("tests/temp/dynamic_changed.txt", "New").unwrap();
            assert!(res.changed());
        }
    }

    #[test]
    fn reload() {
        std::fs::write("tests/temp/dynamic_reload.txt", "Old").unwrap();

        let mut res = resource_str!("tests/temp/dynamic_reload.txt");
        std::fs::write("tests/temp/dynamic_reload.txt", "New").unwrap();

        assert_eq!(res.as_ref(), "Old");
        res.reload();
        assert_eq!(res.as_ref(), "New");
    }

    #[test]
    fn reload_if_changed() {
        // For some reason, `changed()` doesn't seem to work on Travis.
        if option_env!("TRAVIS").is_none() {
            std::fs::write("tests/temp/dynamic_reload_if_changed.txt", "Old").unwrap();

            let mut res = resource_str!("tests/temp/dynamic_reload_if_changed.txt");
            assert!(!res.reload_if_changed());
            assert_eq!(res.as_ref(), "Old");

            std::fs::write("tests/temp/dynamic_reload_if_changed.txt", "New").unwrap();
            assert!(res.reload_if_changed());
            assert_eq!(res.as_ref(), "New");
        }
    }
}