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},
};
#[derive(Debug, PartialEq)]
#[derive(Debug)]
#[cfg_attr(test, derive(PartialEq))]
#[repr(u8)]
/// A statement that can be executed
pub enum Statement {
@ -56,7 +57,8 @@ pub enum Statement {
InspectModel(Entity),
}
#[derive(Debug, PartialEq)]
#[derive(Debug)]
#[cfg_attr(test, derive(PartialEq))]
pub enum Entity {
Current(RawSlice),
Full(RawSlice, RawSlice),
@ -66,7 +68,8 @@ impl Entity {
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
pub struct FieldConfig {
/// the types of the fields

@ -328,7 +328,10 @@ impl<'a> Lexer<'a> {
match unsafe { self.deref_cursor() } {
byte if byte.is_ascii_alphabetic() => {
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()),
None => tokens.push(Token::Identifier(id)),
}

@ -35,22 +35,28 @@ mod tests;
// re-export
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 {
ptr: *const u8,
len: usize,
}
impl Debug for RawSlice {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
write!(f, "{}", String::from_utf8_lossy(self.as_slice()))
#[cfg(test)]
impl fmt::Debug for RawSlice {
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 {
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 {
Self { ptr, len }
}
pub fn as_slice(&self) -> &[u8] {
unsafe { slice::from_raw_parts(self.ptr, self.len) }
pub unsafe fn as_slice(&self) -> &[u8] {
slice::from_raw_parts(self.ptr, self.len)
}
pub const fn len(&self) -> usize {
self.len

Loading…
Cancel
Save