Adding the basic c_types as their own file section.

Also, added the to_c_string and from_c_string functions. Their
location needs to be determined.
This commit is contained in:
Myrddin Dundragon 2017-01-24 14:55:42 -05:00
parent ca780e71ef
commit 26097e8403
4 changed files with 110 additions and 4 deletions

View File

@ -9,5 +9,10 @@ documentation = ""
keywords = ["binding", "c types", "c enums", "c flags", "c strings"]
[features]
default = []
use_std = []
[dependencies.scribe]
git = "https://gitlab.com/CyberMages/scribe.git"

View File

@ -1,5 +1,5 @@
use std::ffi::{CStr, CString};
use std::os::raw::c_char;
//use std::ffi::{CStr, CString};
//use super::c_types::CChar;
@ -8,12 +8,16 @@ pub const EMPTY_STRING: &'static str = "";
/*
TODO: from_c_string should be moved to wherever our String class will
reside.
/// Creates a new String from the data in the pointer.
///
/// Any conversion errors will result in an Empty
/// String being returned. Option is not used here for
/// simplicities sake.
pub fn from_c_string(c_string: *const c_char) -> String
pub fn from_c_string(c_string: *const CChar) -> String
{
unsafe
{
@ -21,6 +25,12 @@ pub fn from_c_string(c_string: *const c_char) -> String
}
}
*/
/*
TODO: Determine if a CString class can be created at this level if
there is no allocation available. The answer is probably not,
but it needs to be investigated.
/// Creates a CString from a given str.
///
/// This may be "" if there is an UTF8 conversion error.
@ -59,3 +69,5 @@ pub fn to_c_string<P>(string: P) -> CString
}
}
}
*/

70
src/c_types.rs Normal file
View File

@ -0,0 +1,70 @@
//! This defines the primitive types that may be seen
//! when creating 'C' bindings.
//!
//! There are several places where the target architecture and the OS
//! require special consideration. These are handled
//! with configuration statements.
// Character types.
#[cfg(any(target_os="android", target_os="emscripten",
all(target_os="linux",
any(target_arch="aarch64", target_arch="arm",
target_arch="powerpc", target_arch="powerpc64"))))]
pub type CChar = u8;
#[cfg(not(any(target_os="android", target_os="emscripten",
all(target_os="linux",
any(target_arch="aarch64", target_arch="arm",
target_arch="powerpc", target_arch="powerpc64")))))]
pub type CChar = i8;
pub type CIChar = i8;
pub type CUChar = u8;
// Integer types.
#[cfg(any(target_pointer_width="32", target_os="windows"))]
pub type CLong = i32;
#[cfg(any(target_pointer_width="32", target_os="windows"))]
pub type CULong = u32;
#[cfg(all(target_pointer_width="64", not(target_os="windows")))]
pub type CLong = i32;
#[cfg(all(target_pointer_width="64", not(target_os="windows")))]
pub type CULong = u32;
pub type CShort = i16;
pub type CUShort = u16;
pub type CInt = i32;
pub type CUInt = u32;
pub type CLongLong = u64;
pub type CULongLong = u64;
pub type CFloat = f32;
pub type CDouble = f64;
// Void types.
/// To return void from a 'C' function it should return an empty tuple ().
/// This type is just a descriptive version of that. **DO NOT** use this
/// as an argument to a function. Use the c_void type for that.
pub type CReturnVoid = ();
/// This acts as a 'C' void pointer to pass to functions. **DO NOT**
/// use this as a return type from a 'C' function. If a 'C' function
/// returns void, then use the c_return_void type for that.
#[repr(u8)]
pub enum CVoid
{
#[doc(hidden)]
_Variant,
#[doc(hidden)]
_Variant2
}

View File

@ -1,6 +1,19 @@
//! The Binding library is a place where common macros and functions
//! that are used when interfacing with C libraries can be stored
//! for use amoungst all of CyberMages LLC's projects.
// Handle using the core or the std of Rust depending on the chosen feature.
#![cfg_attr(not(feature="use_std"), no_std)]
// This crate can only use parts of Rust that are in both the core
// and the standard library. This way the logging system will work for
// libraries that use either.
//
// This is handled by coding using the std library and referencing the core
// library as the std library if the core library is desired.
#[cfg(not(feature="use_std"))]
extern crate core as std;
#[macro_use]
extern crate scribe;
@ -10,11 +23,17 @@ extern crate scribe;
mod c_enum;
mod c_flags;
mod c_string;
mod c_types;
// Raw platform data retrieval.
mod raw;
pub use self::c_string::{EMPTY_STRING, from_c_string, to_c_string};
pub use self::c_string::EMPTY_STRING;
//pub use self::c_string::{from_c_string, to_c_string};
pub use self::c_types::{CChar, CIChar, CUChar};
pub use self::c_types::{CShort, CUShort, CInt, CUInt, CLong, CULong};
pub use self::c_types::{CLongLong, CULongLong, CFloat, CDouble};
pub use self::c_types::{CVoid, CReturnVoid};
pub use self::raw::{AsRaw, AsRawMut, AsRawPtr, AsRawMutPtr, FromRaw, IntoRaw};