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

Overview

::ext-trait

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 ext_trait;
    
    #[extension(trait Also)]
    impl 
         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 ext_trait;
    
    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

    Result<(), Box > { let file_contents = ::std::fs::read_to_string("some/file") .context("Error when opening some/file")? ; // … Ok(()) } ">
    #[macro_use]
    extern crate ext_trait;
    
    use ::std::{error::Error, fmt::Display};
    
    #[extension(trait Context)]
    impl 
          Result
          
            {
        
           fn 
           context (
           self, prefix: 
           impl Display)
          -> 
           Result
           
            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!

You might also like...
The lambda-chaos-extension allows you to inject faults into Lambda functions without modifying the function code.
The lambda-chaos-extension allows you to inject faults into Lambda functions without modifying the function code.

Chaos Extension - Seamless, Universal & Lightning-Fast The lambda-chaos-extension allows you to inject faults into Lambda functions without modifying

Cargo extension to recycle outdated build artifacts

cargo gc Cargo extension to recycle outdated build artifacts. And try the best to avoid recompilation. Usage Install it with cargo: cargo install carg

A low-ish level tool for easily writing and hosting WASM based plugins.

A low-ish level tool for easily writing and hosting WASM based plugins. The goal of wasm_plugin is to make communicating across the host-plugin bounda

Convert Hygea calendar to an iCal file to easily import it to Google Calendar (Rust version)

Hygea to iCal Goal Hygea provides a calendar via PDF and an application called Recycle. I just wanted to use an iCal file to import it in my calendar.

Easily sync your clipboard between devices. This is a work in progress app.

Clipboard Sync Description Easily sync your clipboard between devices. This is a work in progress app. Stack Frontend: React Tauri isomorphic-ws TSX,

PM-Tools - a simple Rust util to easily create server directories

PM-Tools PM-Tools is a simple Rust util to easily create server directories or plugins without the hassle of unzipping or creating directories Progres

Easily customizable command runner made with Rust 🦀
Easily customizable command runner made with Rust 🦀

Easily customizable command runner made with Rust 🦀 📦 Usage Install by the following command: cargo install rxe Or build from the source. git clone

Easily add emojis to your git commit messages 😎
Easily add emojis to your git commit messages 😎

gimoji A CLI tool that makes it easy to add emojis to your git commit messages. It's very similar to (and is based on) gitmoji-cli but written in Rust

Cargo subcommand to easily bootstrap nocode applications. Write nothing; deploy nowhere.

cargo-nocode No code is the best way to write secure and reliable applications. Write nothing; deploy nowhere. cargo-nocode aims to bring the nocode a

Comments
  • Fails to compile with asan on nightly

    Fails to compile with asan on nightly

    ~$ RUSTFLAGS=-Zsanitizer=address cargo test
       Compiling proc-macro2 v1.0.43
       Compiling unicode-ident v1.0.3
       Compiling quote v1.0.21
       Compiling syn v1.0.99
       Compiling ext-trait-proc_macros v1.0.1
       Compiling ext-trait v1.0.1
    error: /.../target/debug/deps/libext_trait_proc_macros-7a93cbfff318ae9a.so: undefined symbol: __asan_option_detect_stack_use_after_return
      --> ~/.cargo/registry/src/github.com-1ecc6299db9ec823/ext-trait-1.0.1/src/lib.rs:35:11
       |
    35 | pub use ::ext_trait_proc_macros::extension;
       |           ^^^^^^^^^^^^^^^^^^^^^
    
    error: could not compile `ext-trait` due to previous error
    
    question 
    opened by changhe3 2
Releases(v1.0.0)
Owner
Daniel Henry-Mantilla
https://danielhenrymantilla.github.io
Daniel Henry-Mantilla
miette is a diagnostic library for Rust. It includes a series of traits/protocols that allow you to hook into its error reporting facilities, and even write your own error reports!

miette is a diagnostic library for Rust. It includes a series of traits/protocols that allow you to hook into its error reporting facilities, and even write your own error reports!

Kat Marchán 1.2k Jan 1, 2023
Async `TryFrom/TryInto` traits

async-convert Async TryFrom/TryInto traits API Docs | Releases | Contributing Installation $ cargo add async-convert Safety This crate uses #![deny(un

Yosh 4 Mar 4, 2022
Various extention traits for providing asynchronous higher-order functions

async-hofs Various extention traits for providing asynchronous higher-order functions. // This won't make any name conflicts since all imports inside

かわえもん 5 Jun 28, 2022
Stdto provides a set of functional traits for conversion between various data representations.

Stdto stdto provides a set of functional traits for conversion between various data representations. | Examples | Docs | Latest Note | stdto = "0.13.0

Doha Lee 5 Dec 21, 2022
Tons of extension utility functions for Rust

LazyExt Tons of extension utility functions for Rust. English | 简体中文 Status Name Status Crate Documents Introduction lazyext-slice Alpha Thousands of

Al Liu 2 Dec 5, 2022
A lambda extension to hot reload parameters from SSM Parameter Store, Secrets Manager, DynamoDB, AppConfig

A lambda extension to hot reload parameters from SSM Parameter Store, Secrets Manager, DynamoDB, AppConfig

Jake Scott 7 Jun 12, 2022
Searchbuddy is a browser extension that lets you chat with people that are searching for what you're searching for.

searchbuddy Make friends while searching! Searchbuddy is a browser extension that lets you chat with people that are searching for what you're searchi

Joseph Gerber 14 May 23, 2022
Rust Lambda Extension for any Runtime to preload SSM Parameters as 🔐 Secure Environment Variables!

?? Crypteia Rust Lambda Extension for any Runtime to preload SSM Parameters as Secure Environment Variables! Super fast and only performaned once duri

Custom Ink 34 Jan 7, 2023
Extension registry for Lapce Registry

Lapce Registry This is the software running the lapce plugin registry, this manages and hosts plugins that the community uploads. Run the registry loc

Lapce 15 Dec 4, 2022
An experimental Athena extension for DuckDB 🐤

DuckDB Athena Extension WARNING This is a work in progress - things may or may not work as expected ??‍♂️ Limitations Only the default database is sup

Damon P. Cortesi 34 Apr 3, 2023