#traits #extension #impl #methods #annotations #custom

extension-traits

Annotation to easily define ad-hoc / one-shot extension traits

4 releases (stable)

2.0.0 Jul 11, 2024
2.0.0-rc1 Jul 10, 2024
1.0.1 Jan 18, 2022

#1021 in Rust patterns

Download history 19871/week @ 2024-10-30 21279/week @ 2024-11-06 19397/week @ 2024-11-13 25052/week @ 2024-11-20 30362/week @ 2024-11-27 43939/week @ 2024-12-04 43777/week @ 2024-12-11 29227/week @ 2024-12-18 12273/week @ 2024-12-25 26158/week @ 2025-01-01 53044/week @ 2025-01-08 46870/week @ 2025-01-15 49379/week @ 2025-01-22 56986/week @ 2025-01-29 67687/week @ 2025-02-05 52146/week @ 2025-02-12

233,794 downloads per month
Used in 86 crates (13 directly)

Zlib OR MIT OR Apache-2.0

15KB
53 lines

::extension-traits

Annotation to easily define ad-hoc / one-shot extension traits.

Repository Latest version Documentation MSRV unsafe forbidden License CI

Examples

  • Also

    #[macro_use]
    extern crate extension_traits;
    
    #[extension(trait Also)]
    impl<T> T {
        fn also (mut self, f: impl FnOnce(&mut Self))
          -> Self
        {
            f(&mut self);
            self
        }
    }
    
    fn main ()
    {
        use ::std::{collections::HashMap, ops::Not};
    
        let /* immut */ map = HashMap::with_capacity(2).also(|m| {
            m.insert("foo", 42);
            m.insert("bar", 27);
        });
        assert!(map.contains_key("foo"));
        assert!(map.contains_key("bar"));
        assert!(map.contains_key("baz").not());
    }
    
  • WithPath

    #[macro_use]
    extern crate extension_traits;
    
    use ::std::{error::Error, path::{Path, PathBuf}};
    
    #[extension(trait WithPath)]
    impl PathBuf {
        fn with (mut self, segment: impl AsRef<Path>)
          -> PathBuf
        {
            self.push(segment);
            self
        }
    }
    
    fn main ()
      -> Result<(), Box<dyn Error>>
    {
        let some_dir = PathBuf::from(::std::env::var("MY_LIB_SOME_DIR")?);
        // Contrary to chaining `.join()`, this reuses the memory!
        let some_subdir = some_dir.with("some").with("sub").with("dir");
        //
        Ok(())
    }
    
  • Context

    #[macro_use]
    extern crate extension_traits;
    
    use ::std::{error::Error, fmt::Display};
    
    #[extension(trait Context)]
    impl<Ok, Err : Display> Result<Ok, Err> {
        fn context (self, prefix: impl Display)
          -> Result<Ok, String>
        {
            self.map_err(|err| format!("{}: {}", prefix, err))
        }
    }
    
    fn main ()
      -> Result<(), Box<dyn Error>>
    {
        let file_contents =
            ::std::fs::read_to_string("some/file")
                .context("Error when opening some/file")?
        ;
        //
        Ok(())
    }
    

Similar to https://docs.rs/extension-trait, but for the following:

Features

  • Supports generics (see Context)

  • search/grep 'trait TraitName'-friendly!

Dependencies

~1.5MB
~37K SLoC

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy