Skip to content

Add into_static for schema::Document #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 23, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/schema/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,26 @@ pub struct Document<'a, T: Text<'a>>
pub definitions: Vec<Definition<'a, T>>,
}

impl<'a> Document<'a, String> {
pub fn into_static(self) -> Document<'static, String> {
// To support both reference and owned values in the AST,
// all string data is represented with the ::common::Str<'a, T: Text<'a>>
// wrapper type.
// This type must carry the liftetime of the schema string,
// and is stored in a PhantomData value on the Str type.
// When using owned String types, the actual lifetime of
// the Ast nodes is 'static, since no references are kept,
// but the nodes will still carry the input lifetime.
// To continue working with Document<String> in a owned fasion
// the lifetime needs to be transmuted to 'static.
//
// This is safe because no references are present.
// Just the PhantomData lifetime reference is transmuted away.
unsafe { std::mem::transmute::<_, Document<'static, String>>(self) }
}
}


#[derive(Debug, Clone, PartialEq)]
pub enum Definition<'a, T: Text<'a>> {
SchemaDefinition(SchemaDefinition<'a, T>),
Expand Down