Skip to content
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

Move rust code out of build.rs and into lib.rs #3458

Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

249 changes: 1 addition & 248 deletions rust/ruby-prism/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -555,254 +555,7 @@ use std::ptr::NonNull;

#[allow(clippy::wildcard_imports)]
use ruby_prism_sys::*;

/// A range in the source file.
pub struct Location<'pr> {{
parser: NonNull<pm_parser_t>,
pub(crate) start: *const u8,
pub(crate) end: *const u8,
marker: PhantomData<&'pr [u8]>
}}

impl<'pr> Location<'pr> {{
/// Returns a byte slice for the range.
#[must_use]
pub fn as_slice(&self) -> &'pr [u8] {{
unsafe {{
let len = usize::try_from(self.end.offset_from(self.start)).expect("end should point to memory after start");
std::slice::from_raw_parts(self.start, len)
}}
}}

/// Return a Location from the given `pm_location_t`.
#[must_use]
pub(crate) const fn new(parser: NonNull<pm_parser_t>, loc: &'pr pm_location_t) -> Location<'pr> {{
Location {{ parser, start: loc.start, end: loc.end, marker: PhantomData }}
}}

/// Return a Location starting at self and ending at the end of other.
/// Returns None if both locations did not originate from the same parser,
/// or if self starts after other.
#[must_use]
pub fn join(&self, other: &Location<'pr>) -> Option<Location<'pr>> {{
if self.parser != other.parser || self.start > other.start {{
None
}} else {{
Some(Location {{ parser: self.parser, start: self.start, end: other.end, marker: PhantomData }})
}}
}}

/// Return the start offset from the beginning of the parsed source.
#[must_use]
pub fn start_offset(&self) -> usize {{
unsafe {{
let parser_start = (*self.parser.as_ptr()).start;
usize::try_from(self.start.offset_from(parser_start)).expect("start should point to memory after the parser's start")
}}
}}

/// Return the end offset from the beginning of the parsed source.
#[must_use]
pub fn end_offset(&self) -> usize {{
unsafe {{
let parser_start = (*self.parser.as_ptr()).start;
usize::try_from(self.end.offset_from(parser_start)).expect("end should point to memory after the parser's start")
}}
}}
}}

impl std::fmt::Debug for Location<'_> {{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {{
let slice: &[u8] = self.as_slice();

let mut visible = String::new();
visible.push('"');

for &byte in slice {{
let part: Vec<u8> = std::ascii::escape_default(byte).collect();
visible.push_str(std::str::from_utf8(&part).unwrap());
}}

visible.push('"');
write!(f, "{{visible}}")
}}
}}

/// An iterator over the nodes in a list.
pub struct NodeListIter<'pr> {{
parser: NonNull<pm_parser_t>,
pointer: NonNull<pm_node_list>,
index: usize,
marker: PhantomData<&'pr mut pm_node_list>
}}

impl<'pr> Iterator for NodeListIter<'pr> {{
type Item = Node<'pr>;

fn next(&mut self) -> Option<Self::Item> {{
if self.index >= unsafe {{ self.pointer.as_ref().size }} {{
None
}} else {{
let node: *mut pm_node_t = unsafe {{ *(self.pointer.as_ref().nodes.add(self.index)) }};
self.index += 1;
Some(Node::new(self.parser, node))
}}
}}
}}

/// A list of nodes.
pub struct NodeList<'pr> {{
parser: NonNull<pm_parser_t>,
pointer: NonNull<pm_node_list>,
marker: PhantomData<&'pr mut pm_node_list>
}}

impl<'pr> NodeList<'pr> {{
/// Returns an iterator over the nodes.
#[must_use]
pub fn iter(&self) -> NodeListIter<'pr> {{
NodeListIter {{
parser: self.parser,
pointer: self.pointer,
index: 0,
marker: PhantomData
}}
}}
}}

impl std::fmt::Debug for NodeList<'_> {{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {{
write!(f, "{{:?}}", self.iter().collect::<Vec<_>>())
}}
}}

/// A handle for a constant ID.
pub struct ConstantId<'pr> {{
parser: NonNull<pm_parser_t>,
id: pm_constant_id_t,
marker: PhantomData<&'pr mut pm_constant_id_t>
}}

impl<'pr> ConstantId<'pr> {{
fn new(parser: NonNull<pm_parser_t>, id: pm_constant_id_t) -> Self {{
ConstantId {{ parser, id, marker: PhantomData }}
}}

/// Returns a byte slice for the constant ID.
///
/// # Panics
///
/// Panics if the constant ID is not found in the constant pool.
#[must_use]
pub fn as_slice(&self) -> &'pr [u8] {{
unsafe {{
let pool = &(*self.parser.as_ptr()).constant_pool;
let constant = &(*pool.constants.add((self.id - 1).try_into().unwrap()));
std::slice::from_raw_parts(constant.start, constant.length)
}}
}}
}}

impl std::fmt::Debug for ConstantId<'_> {{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {{
write!(f, "{{:?}}", self.id)
}}
}}

/// An iterator over the constants in a list.
pub struct ConstantListIter<'pr> {{
parser: NonNull<pm_parser_t>,
pointer: NonNull<pm_constant_id_list_t>,
index: usize,
marker: PhantomData<&'pr mut pm_constant_id_list_t>
}}

impl<'pr> Iterator for ConstantListIter<'pr> {{
type Item = ConstantId<'pr>;

fn next(&mut self) -> Option<Self::Item> {{
if self.index >= unsafe {{ self.pointer.as_ref().size }} {{
None
}} else {{
let constant_id: pm_constant_id_t = unsafe {{ *(self.pointer.as_ref().ids.add(self.index)) }};
self.index += 1;
Some(ConstantId::new(self.parser, constant_id))
}}
}}
}}

/// A list of constants.
pub struct ConstantList<'pr> {{
/// The raw pointer to the parser where this list came from.
parser: NonNull<pm_parser_t>,

/// The raw pointer to the list allocated by prism.
pointer: NonNull<pm_constant_id_list_t>,

/// The marker to indicate the lifetime of the pointer.
marker: PhantomData<&'pr mut pm_constant_id_list_t>
}}

impl<'pr> ConstantList<'pr> {{
/// Returns an iterator over the constants in the list.
#[must_use]
pub fn iter(&self) -> ConstantListIter<'pr> {{
ConstantListIter {{
parser: self.parser,
pointer: self.pointer,
index: 0,
marker: PhantomData
}}
}}
}}

impl std::fmt::Debug for ConstantList<'_> {{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {{
write!(f, "{{:?}}", self.iter().collect::<Vec<_>>())
}}
}}

/// A handle for an arbitarily-sized integer.
pub struct Integer<'pr> {{
/// The raw pointer to the integer allocated by prism.
pointer: *const pm_integer_t,

/// The marker to indicate the lifetime of the pointer.
marker: PhantomData<&'pr mut pm_constant_id_t>
}}

impl<'pr> Integer<'pr> {{
fn new(pointer: *const pm_integer_t) -> Self {{
Integer {{ pointer, marker: PhantomData }}
}}
}}

impl std::fmt::Debug for Integer<'_> {{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {{
write!(f, "{{:?}}", self.pointer)
}}
}}

impl TryInto<i32> for Integer<'_> {{
type Error = ();

fn try_into(self) -> Result<i32, Self::Error> {{
let negative = unsafe {{ (*self.pointer).negative }};
let length = unsafe {{ (*self.pointer).length }};

if length == 0 {{
i32::try_from(unsafe {{ (*self.pointer).value }}).map_or(Err(()), |value|
if negative {{
Ok(-value)
}} else {{
Ok(value)
}}
)
}} else {{
Err(())
}}
}}
}}
use crate::{{ConstantId, ConstantList, Integer, Location, NodeList}};
"#
)?;

Expand Down
Loading
Loading