Concatenating idents in macros


#1

I have the following macro which is generating C-compatible functions for exporting via cbindgen:

distinst_disk!(disk,
    /// Adds a new partition to the disk's partition scheme, using a PartitionBuilder object.
    distinst_disk_add_partition(partition: *mut DistinstPartitionBuilder) {
        disk.add_partition(*Box::from_raw(partition as *mut PartitionBuilder))
    }

    /// Removes an existing partition from the disk, via the partition's ID.
    distinst_disk_remove_partition(partition: libc::c_int) {
        disk.remove_partition(partition)
    }

    /// Resizes an existing partition on the disk, to the new length specified.
    distinst_disk_resize_partition(partition: libc::c_int, length: libc::uint64_t) {
        disk.resize_partition(partition, length)
    }

    /// Moves an existing partition on the disk, to the new start sector specified.
    distinst_disk_move_partition(partition: libc::c_int, start: libc::uint64_t) {
        disk.move_partition(partition, start)        
    }

    /// Formats an existing partition using the file system specified.
    distinst_disk_format_partition(partition: libc::c_int, fs: DISTINST_FILE_SYSTEM_TYPE) {
        let fs = match Option::<FileSystemType>::from(fs) {
            Some(fs) => fs,
            None => {
                info!("file system type required");
                return -1;
            }
        };

        disk.format_partition(partition, fs)
    }

    /// Clobbers all partitions on the disk, and writes a new partition table.
    distinst_disk_mklabel(table: DISTINST_PARTITION_TABLE) {
        let table = match table {
            DISTINST_PARTITION_TABLE::GPT => PartitionTable::Gpt,
            DISTINST_PARTITION_TABLE::MSDOS => PartitionTable::Msdos,
            _ => return -1,
        };

        disk.mklabel(table)
    }

    /// Commits all changes to the disk
    distinst_disk_commit() {
        disk.commit()
    }   
);

Is there currently a way to not require typing the full distinst_disk_*, so that each function can be written as just commit() / add_partitition()?


#2

In theory you’d want concat_idents!, but it doesn’t really work. See the complaints in:


#3

If you implement distinst_disk! as a procedural macro (either using the unstable #[proc_macro] or using stable proc-macro-hack) then it can do this.