kaleidawave / source-map

Utilities for building source maps (v3) for a compiler
MIT License
5 stars 2 forks source link

Add public SourceId constructor #6

Open stefnotch opened 3 months ago

stefnotch commented 3 months ago

I would like to implement the FileSystem trait for my compiler instead of using one of the existing datastructures. However, that is impossible without the ability to create a new SourceId.

Could a public constructor for SourceId be added, or is there another option that I should be pursuing?

kaleidawave commented 3 months ago

👍 Yes I see it is not possible to create an impl of the FileSystem trait because one of the required methods requires creating a SourceId and that is not currently possible as the field is pub(crate).

Interested in what you are using the FileSystem trait for (I guess for something related to build a source map in which the current logic needs to get contents and LineStarts from files)?

As a temporary measure you can create a SourceId using unsafe+transmute

fn new_source_id(id: u16) -> source_map::SourceId {
    unsafe { std::mem::transmute(id) }
}

LMK how it goes with this temporary function and whether you find any other API changes that you want to use this in your compiler.

Will have a look at a adding something like SourceId::new this week. AFAIK this crate is only used by one of my own projects. Would love to see it used in other projects!