lib/faster_path.rb in faster_path-0.1.13 vs lib/faster_path.rb in faster_path-0.2.0
- old
+ new
@@ -2,112 +2,90 @@
require 'pathname'
require "ffi"
require 'faster_path/asset_resolution'
module FasterPath
+ FFI_LIBRARY = begin
+ prefix = Gem.win_platform? ? "" : "lib"
+ "#{File.expand_path("../target/release/", __dir__)}/#{prefix}faster_path.#{FFI::Platform::LIBSUFFIX}"
+ end
+ require 'fiddle'
+ library = Fiddle.dlopen(FFI_LIBRARY)
+ Fiddle::Function.
+ new(library['Init_faster_pathname'], [], Fiddle::TYPE_VOIDP).
+ call
+
+ FasterPathname.class_eval do
+ private :add_trailing_separator
+ private :basename
+ private :chop_basename
+ private :dirname
+ private :entries
+ private :extname
+ private :plus
+ end
+
def self.rust_arch_bits
Rust.rust_arch_bits
end
def self.ruby_arch_bits
1.size * 8
end
- # Spec to Pathname#absolute?
def self.absolute?(pth)
- Rust.is_absolute(pth)
+ FasterPathname.allocate.send(:absolute?, pth)
end
- # Spec to Pathname#directory?
- def self.directory?(pth)
- Rust.is_directory(pth)
+ def self.add_trailing_separator(pth)
+ FasterPathname.allocate.send(:add_trailing_separator, pth)
end
- # Spec to Pathname#relative?
- def self.relative?(pth)
- Rust.is_relative(pth)
+ def self.blank?(str)
+ "#{str}".strip.empty?
end
- def self.dirname(pth)
- Rust.dirname(pth)
+ def self.basename(pth, ext="")
+ FasterPathname.allocate.send(:basename, pth, ext)
end
- # Spec to Pathname#chop_basename
- # WARNING! Pathname#chop_basename in STDLIB doesn't handle blank strings correctly!
- # This implementation correctly handles blank strings just as Pathname had intended
- # to handle non-path strings.
def self.chop_basename(pth)
- d = Rust.dirname_for_chop(pth)
- b = Rust.basename_for_chop(pth)
- [d, b] unless Rust.both_are_blank(d, b)
+ result = FasterPathname.allocate.send(:chop_basename, pth)
+ result unless result.empty?
end
- def self.blank?(str)
- Rust.is_blank(str)
+ def self.directory?(pth)
+ FasterPathname.allocate.send(:directory?, pth)
end
- def self.basename(pth, ext="")
- Rust.basename(pth, ext)
+ def self.dirname(pth)
+ FasterPathname.allocate.send(:dirname, pth)
end
- def self.plus(pth, pth2)
- Rust.plus(pth, pth2)
+ def self.entries(pth)
+ FasterPathname.allocate.send(:entries, pth)
end
- def self.add_trailing_separator(pth)
- Rust.add_trailing_separator(pth)
+ def self.extname(pth)
+ FasterPathname.allocate.send(:extname, pth)
end
def self.has_trailing_separator?(pth)
- Rust.has_trailing_separator(pth)
+ FasterPathname.allocate.send(:has_trailing_separator?, pth)
end
- def self.extname(pth)
- Rust.extname(pth)
+ def self.plus(pth, pth2)
+ FasterPathname.allocate.send(:plus, pth, pth2)
end
- def self.entries(pth)
- Array(Rust.entries(pth))
+ def self.relative?(pth)
+ FasterPathname.allocate.send(:relative?, pth)
end
- # EXAMPLE
- # def self.one_and_two
- # Rust.one_and_two.to_a
- # end
-
module Rust
extend FFI::Library
- ffi_lib begin
- prefix = Gem.win_platform? ? "" : "lib"
- "#{File.expand_path("../target/release/", __dir__)}/#{prefix}faster_path.#{FFI::Platform::LIBSUFFIX}"
- end
+ ffi_lib ::FasterPath::FFI_LIBRARY
- class FromRustArray < FFI::Struct
- layout :len, :size_t, # dynamic array layout
- :data, :pointer #
-
- def to_a
- self[:data].get_array_of_string(0, self[:len]).compact
- end
- end
-
attach_function :rust_arch_bits, [], :int32
- attach_function :is_absolute, [ :string ], :bool
- attach_function :is_directory, [ :string ], :bool
- attach_function :is_relative, [ :string ], :bool
- attach_function :is_blank, [ :string ], :bool
- attach_function :both_are_blank, [ :string, :string ], :bool
- attach_function :basename, [ :string, :string ], :string
- attach_function :plus, [ :string, :string ], :string
- attach_function :dirname, [ :string ], :string
- attach_function :basename_for_chop, [ :string ], :string # decoupling behavior
- attach_function :dirname_for_chop, [ :string ], :string # decoupling behavior
- attach_function :add_trailing_separator, [ :string ], :string
- attach_function :has_trailing_separator, [ :string ], :bool
- attach_function :extname, [ :string ], :string
- attach_function :entries, [ :string ], FromRustArray.by_value
-
- # EXAMPLE
- # attach_function :one_and_two, [], FromRustArray.by_value
end
private_constant :Rust
end