Skip to content

Commit

Permalink
Implement From<oxidized::*> for hackrs Decl structs
Browse files Browse the repository at this point in the history
Summary:
Relatively straightforward conversion from `from_oxidized_by_ref` but from oxidized structs instead.

- Added a `From<oxidized::pos::Pos>` impl because it made a bunch of `.into()`s much cleaner
  - Ditto for some types from owned Strings
- Split `map` into `map_k` and `map_kv` but fixed their inputs to just the relevant sets and maps, leaving outputs iterators
  - This makes the code simpler to follow imo -- don't need to call into_iter on their args, but the outputs are flexible
  - only 4 occurrences of the `.into_iter.map(...).collect()` remain where the inner closure has a little more going on, the rest call the helpers

Differential Revision: D68520144

fbshipit-source-id: 0a3ec1434ad7e075e7bdff795aab074c0188ced4
  • Loading branch information
vassilmladenov authored and facebook-github-bot committed Jan 23, 2025
1 parent 2cb1d52 commit a3df344
Show file tree
Hide file tree
Showing 5 changed files with 897 additions and 0 deletions.
27 changes: 27 additions & 0 deletions hphp/hack/src/hackrs/pos/pos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub trait Pos:
+ std::fmt::Debug
+ Serialize
+ DeserializeOwned
+ for<'a> From<oxidized::pos::Pos>
+ for<'a> From<&'a oxidized::pos::Pos>
+ for<'a> From<&'a oxidized_by_ref::pos::Pos<'a>>
+ for<'a> ToOxidizedByRef<'a, Output = &'a oxidized_by_ref::pos::Pos<'a>>
Expand Down Expand Up @@ -247,6 +248,12 @@ impl From<BPos> for oxidized::pos::Pos {
}
}

impl From<oxidized::pos::Pos> for BPos {
fn from(pos: oxidized::pos::Pos) -> Self {
Self::from_ast(&pos)
}
}

impl<'a> From<&'a oxidized::pos::Pos> for BPos {
fn from(pos: &'a oxidized::pos::Pos) -> Self {
Self::from_ast(pos)
Expand Down Expand Up @@ -411,6 +418,12 @@ impl EqModuloPos for NPos {
}
}

impl From<oxidized::pos::Pos> for NPos {
fn from(pos: oxidized::pos::Pos) -> Self {
Self::from_ast(&pos)
}
}

impl<'a> From<&'a oxidized::pos::Pos> for NPos {
fn from(pos: &'a oxidized::pos::Pos) -> Self {
Self::from_ast(pos)
Expand Down Expand Up @@ -487,6 +500,13 @@ impl<S: Copy, P> Positioned<S, P> {
}
}

impl<S: From<String>, P: Pos> From<oxidized::ast_defs::Id> for Positioned<S, P> {
fn from(pos_id: oxidized::ast_defs::Id) -> Self {
let oxidized::ast_defs::Id(pos, id) = pos_id;
Self::new(Pos::from_ast(&pos), S::from(id))
}
}

impl<'a, S: From<&'a str>, P: Pos> From<&'a oxidized::ast_defs::Id> for Positioned<S, P> {
fn from(pos_id: &'a oxidized::ast_defs::Id) -> Self {
let oxidized::ast_defs::Id(pos, id) = pos_id;
Expand All @@ -501,6 +521,13 @@ impl<'a, S: From<&'a str>, P: Pos> From<oxidized_by_ref::ast_defs::Id<'a>> for P
}
}

impl<S: From<String>, P: Pos> From<oxidized::typing_defs::PosId> for Positioned<S, P> {
fn from(pos_id: oxidized::typing_defs::PosId) -> Self {
let (pos, id) = pos_id;
Self::new(Pos::from_ast(&pos), S::from(id))
}
}

impl<'a, S: From<&'a str>, P: Pos> From<oxidized_by_ref::typing_defs::PosId<'a>>
for Positioned<S, P>
{
Expand Down
12 changes: 12 additions & 0 deletions hphp/hack/src/hackrs/pos/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,24 @@ impl From<&[u8]> for Bytes {
}
}

impl From<bstr::BString> for Bytes {
fn from(s: bstr::BString) -> Self {
Self::new(s)
}
}

impl From<&bstr::BStr> for Bytes {
fn from(s: &bstr::BStr) -> Self {
Self::new(s)
}
}

impl From<String> for Bytes {
fn from(s: String) -> Self {
Self::new(s)
}
}

impl From<&str> for Bytes {
fn from(s: &str) -> Self {
Self::new(s)
Expand Down
1 change: 1 addition & 0 deletions hphp/hack/src/hackrs/ty/decl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

mod debug;
pub mod folded;
mod from_oxidized;
mod from_oxidized_by_ref;
mod ocamlrep;
mod printer;
Expand Down
Loading

0 comments on commit a3df344

Please sign in to comment.