Hey,

Is there any way to create a macro that allows a Some<T> or T as input?

It’s for creating a Span struct that I’m using:

struct Span {
    line: usize,
    column: usize,
    file_path: Option<String>,
}

…and I have the following macro:

macro_rules! span {
    ($line:expr, $column:expr) => {
        Span {
            line: $line,
            column: $column
            file_path: None,
        }
    };

    ($line:expr, $column:expr, $file_path: expr) => {
        Span {
            line: $line,
            column: $column
            file_path: Some($file_path.to_string()),
        }
    };
}

…which allows me to do this:

let foo = span!(1, 1);
let bar = span!(1, 1, "file.txt");

However, sometimes I don’t want to pass in the file path directly but through a variable that is Option<String>. To do this, I always have to match the variable:

let file_path = Some("file.txt");

let foo = match file_path {
    Some(file_path) => span!(1, 1, file_path),
    None => span!(1, 1),
}

Is there a way which allows me to directly use span!(1, 1, file_path) where file_path could be "file.txt", Some("file.txt") or None?

Thanks in advance!

  • v9CYKjLeia10dZpz88iU@programming.dev
    link
    fedilink
    arrow-up
    0
    ·
    edit-2
    5 months ago

    I made an edit.

    There’s not anything stopping it from supporting Option<&str> though. This would be the implementation

    impl OptionUpgrade for Option<&str> {
        fn upgrade(self) -> Option<String> {
            self.map(|v| v.into())
        }
    }
    

    It’s also possible to just make it generic over Option types

    impl<A: ToString> OptionUpgrade for Option<A> {
        fn upgrade(self) -> Option<String> {
            self.map(|v| v.to_string())
        }
    }
    
    • BB_C@programming.dev
      link
      fedilink
      arrow-up
      0
      ·
      5 months ago

      Yes, but then the concrete type of None literals becomes unknown, which is what I was trying to point out.