Ensure `PartialEq` safety for `RawSlice`

next
Sayan Nandan 2 years ago
parent 2c69f7509f
commit f791fb32a3
No known key found for this signature in database
GPG Key ID: 8BC07A0A4D41DD52

@ -34,7 +34,8 @@ use {
core::{marker::PhantomData, mem::transmute, ptr}, core::{marker::PhantomData, mem::transmute, ptr},
}; };
#[derive(Debug, PartialEq)] #[derive(Debug)]
#[cfg_attr(test, derive(PartialEq))]
#[repr(u8)] #[repr(u8)]
/// A statement that can be executed /// A statement that can be executed
pub enum Statement { pub enum Statement {
@ -56,7 +57,8 @@ pub enum Statement {
InspectModel(Entity), InspectModel(Entity),
} }
#[derive(Debug, PartialEq)] #[derive(Debug)]
#[cfg_attr(test, derive(PartialEq))]
pub enum Entity { pub enum Entity {
Current(RawSlice), Current(RawSlice),
Full(RawSlice, RawSlice), Full(RawSlice, RawSlice),
@ -66,7 +68,8 @@ impl Entity {
const MAX_LENGTH_EX: usize = 65; const MAX_LENGTH_EX: usize = 65;
} }
#[derive(PartialEq, Debug)] #[derive(Debug)]
#[cfg_attr(test, derive(PartialEq))]
/// The field configuration used when declaring the fields for a model /// The field configuration used when declaring the fields for a model
pub struct FieldConfig { pub struct FieldConfig {
/// the types of the fields /// the types of the fields

@ -328,7 +328,10 @@ impl<'a> Lexer<'a> {
match unsafe { self.deref_cursor() } { match unsafe { self.deref_cursor() } {
byte if byte.is_ascii_alphabetic() => { byte if byte.is_ascii_alphabetic() => {
let id = self.scan_ident(); let id = self.scan_ident();
match Keyword::try_from_slice(id.as_slice()) { match Keyword::try_from_slice(unsafe {
// UNSAFE(@ohsayan): The source buffer's presence guarantees that this is correct
id.as_slice()
}) {
Some(kw) => tokens.push(kw.into()), Some(kw) => tokens.push(kw.into()),
None => tokens.push(Token::Identifier(id)), None => tokens.push(Token::Identifier(id)),
} }

@ -35,22 +35,28 @@ mod tests;
// re-export // re-export
pub use ast::Compiler; pub use ast::Compiler;
use core::{fmt::Debug, slice}; #[cfg(test)]
use core::fmt;
use core::slice;
#[cfg_attr(not(test), derive(Debug))]
#[cfg_attr(not(test), derive(PartialEq))]
pub struct RawSlice { pub struct RawSlice {
ptr: *const u8, ptr: *const u8,
len: usize, len: usize,
} }
impl Debug for RawSlice { #[cfg(test)]
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> { impl fmt::Debug for RawSlice {
write!(f, "{}", String::from_utf8_lossy(self.as_slice())) fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", String::from_utf8_lossy(unsafe { self.as_slice() }))
} }
} }
#[cfg(test)]
impl PartialEq for RawSlice { impl PartialEq for RawSlice {
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &Self) -> bool {
self.as_slice() == other.as_slice() unsafe { self.as_slice() == other.as_slice() }
} }
} }
@ -58,8 +64,8 @@ impl RawSlice {
pub const unsafe fn new(ptr: *const u8, len: usize) -> Self { pub const unsafe fn new(ptr: *const u8, len: usize) -> Self {
Self { ptr, len } Self { ptr, len }
} }
pub fn as_slice(&self) -> &[u8] { pub unsafe fn as_slice(&self) -> &[u8] {
unsafe { slice::from_raw_parts(self.ptr, self.len) } slice::from_raw_parts(self.ptr, self.len)
} }
pub const fn len(&self) -> usize { pub const fn len(&self) -> usize {
self.len self.len

Loading…
Cancel
Save